Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CompositeUserContext.php
Go to the documentation of this file.
1 <?php
8 
10 
21 {
25  protected $userContexts = [];
26 
30  protected $chosenUserContext;
31 
38  public function __construct(CompositeHelper $compositeHelper, $userContexts = [])
39  {
40  $userContexts = $compositeHelper->filterAndSortDeclaredComponents($userContexts);
41  foreach ($userContexts as $userContext) {
42  $this->add($userContext['type']);
43  }
44  }
45 
52  protected function add(UserContextInterface $userContext)
53  {
54  $this->userContexts[] = $userContext;
55  return $this;
56  }
57 
61  public function getUserId()
62  {
63  return $this->getUserContext() ? $this->getUserContext()->getUserId() : null;
64  }
65 
69  public function getUserType()
70  {
71  return $this->getUserContext() ? $this->getUserContext()->getUserType() : null;
72  }
73 
79  protected function getUserContext()
80  {
81  if ($this->chosenUserContext === null) {
83  foreach ($this->userContexts as $userContext) {
84  if ($userContext->getUserType() && $userContext->getUserId() !== null) {
85  $this->chosenUserContext = $userContext;
86  break;
87  }
88  }
89  if ($this->chosenUserContext === null) {
90  $this->chosenUserContext = false;
91  }
92  }
94  }
95 }
__construct(CompositeHelper $compositeHelper, $userContexts=[])