Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Consumer.php
Go to the documentation of this file.
1 <?php
7 
9 
28 {
32  protected $urlValidator;
33 
38 
42  protected $dataHelper;
43 
47  private $_dateHelper;
48 
59  public function __construct(
60  \Magento\Framework\Model\Context $context,
61  \Magento\Framework\Registry $registry,
62  \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength $keyLength,
63  \Magento\Framework\Url\Validator $urlValidator,
64  \Magento\Integration\Helper\Oauth\Data $dataHelper,
65  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
66  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
67  array $data = []
68  ) {
69  $this->keyLengthValidator = $keyLength;
70  $this->urlValidator = $urlValidator;
71  $this->dataHelper = $dataHelper;
72  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
73  }
74 
80  protected function _construct()
81  {
82  parent::_construct();
83  $this->_init(\Magento\Integration\Model\ResourceModel\Oauth\Consumer::class);
84  }
85 
93  private function getDateHelper()
94  {
95  if ($this->_dateHelper === null) {
97  ->get(\Magento\Framework\Stdlib\DateTime\DateTime::class);
98  }
99  return $this->_dateHelper;
100  }
101 
107  public function beforeSave()
108  {
109  $this->validate();
110  parent::beforeSave();
111  return $this;
112  }
113 
117  public function validate()
118  {
119  if ($this->getCallbackUrl() || $this->getRejectedCallbackUrl()) {
120  $this->setCallbackUrl(trim($this->getCallbackUrl()));
121  $this->setRejectedCallbackUrl(trim($this->getRejectedCallbackUrl()));
122 
123  if ($this->getCallbackUrl() && !$this->urlValidator->isValid($this->getCallbackUrl())) {
124  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Callback URL'));
125  }
126  if ($this->getRejectedCallbackUrl() && !$this->urlValidator->isValid($this->getRejectedCallbackUrl())) {
127  throw new \Magento\Framework\Exception\LocalizedException(__('Invalid Rejected Callback URL'));
128  }
129  }
130 
131  $this->keyLengthValidator
132  ->setLength(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_KEY)
133  ->setName('Consumer Key');
134  if (!$this->keyLengthValidator->isValid($this->getKey())) {
135  $messages = $this->keyLengthValidator->getMessages();
136  throw new \Magento\Framework\Exception\LocalizedException(__(array_shift($messages)));
137  }
138 
139  $this->keyLengthValidator
140  ->setLength(\Magento\Framework\Oauth\Helper\Oauth::LENGTH_CONSUMER_SECRET)
141  ->setName('Consumer Secret');
142  if (!$this->keyLengthValidator->isValid($this->getSecret())) {
143  $messages = $this->keyLengthValidator->getMessages();
144  throw new \Magento\Framework\Exception\LocalizedException(__(array_shift($messages)));
145  }
146  return true;
147  }
148 
155  public function loadByKey($key)
156  {
157  return $this->load($key, 'key');
158  }
159 
163  public function getKey()
164  {
165  return $this->getData('key');
166  }
167 
171  public function getSecret()
172  {
173  return $this->getData('secret');
174  }
175 
179  public function getCallbackUrl()
180  {
181  return $this->getData('callback_url');
182  }
183 
187  public function getCreatedAt()
188  {
189  return $this->getData('created_at');
190  }
191 
195  public function isValidForTokenExchange()
196  {
197  $expiry = $this->dataHelper->getConsumerExpirationPeriod();
198  $currentTimestamp = $this->getDateHelper()->gmtTimestamp();
199  $updatedTimestamp = $this->getDateHelper()->gmtTimestamp($this->getUpdatedAt());
200  return $expiry > ($currentTimestamp - $updatedTimestamp);
201  }
202 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength $keyLength, \Magento\Framework\Url\Validator $urlValidator, \Magento\Integration\Helper\Oauth\Data $dataHelper, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Consumer.php:59