Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Context.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
22 {
26  const USER_TYPE_ID = 'user_type';
27  const USER_ID = 'user_id';
33  private $userContext;
34 
43  public function __construct(
44  \Magento\Framework\Model\Context $context,
45  \Magento\Framework\Registry $registry,
46  ExtensionAttributesFactory $extensionFactory,
48  UserContextInterface $userContext,
49  array $data = []
50  ) {
51  parent::__construct(
52  $context,
53  $registry,
54  $extensionFactory,
56  );
57  if (isset($data['id'])) {
58  $this->setId($data['id']);
59  }
60  if (isset($data['type'])) {
61  $this->setId($data['type']);
62  }
63  $this->userContext = $userContext;
64  }
65 
71  public function getExtensionAttributes() : \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface
72  {
73  return $this->_getExtensionAttributes();
74  }
75 
82  public function setExtensionAttributes(
83  \Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes
84  ) : ContextInterface {
85  return $this->_setExtensionAttributes($extensionAttributes);
86  }
87 
91  public function getUserId() : int
92  {
93  if (!$this->getData(self::USER_ID)) {
94  $this->setUserId((int) $this->userContext->getUserId());
95  }
96  return (int) $this->getData(self::USER_ID);
97  }
98 
102  public function setUserId(int $userId) : ContextInterface
103  {
104  return $this->setData(self::USER_ID, $userId);
105  }
106 
110  public function getUserType() : int
111  {
112  if (!$this->getData(self::USER_TYPE_ID)) {
113  $this->setUserType($this->userContext->getUserType());
114  }
115  return (int) $this->getData(self::USER_TYPE_ID);
116  }
117 
121  public function setUserType(int $typeId) : ContextInterface
122  {
123  return $this->setData(self::USER_TYPE_ID, $typeId);
124  }
125 }
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, UserContextInterface $userContext, array $data=[])
Definition: Context.php:43
setExtensionAttributes(\Magento\Framework\GraphQl\Query\Resolver\ContextExtensionInterface $extensionAttributes)
Definition: Context.php:82
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)