Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequestLog.php
Go to the documentation of this file.
1 <?php
7 
11 
18 {
22  private $dateTime;
23 
27  private $requestLogConfig;
28 
37  public function __construct(
38  \Magento\Framework\Model\ResourceModel\Db\Context $context,
39  \Magento\Framework\Stdlib\DateTime\DateTime $dateTime,
40  RequestLogConfig $requestLogConfig,
41  $connectionName = null
42  ) {
43  parent::__construct($context, $connectionName);
44  $this->dateTime = $dateTime;
45  $this->requestLogConfig = $requestLogConfig;
46  }
47 
51  protected function _construct()
52  {
53  $this->_init('oauth_token_request_log', 'entity_id');
54  }
55 
59  public function getFailuresCount($userName, $userType)
60  {
61  $select = $this->getConnection()->select();
62  $select->from($this->getMainTable(), 'failures_count')
63  ->where('user_name = :user_name AND user_type = :user_type');
64 
65  return (int)$this->getConnection()->fetchOne($select, ['user_name' => $userName, 'user_type' => $userType]);
66  }
67 
71  public function resetFailuresCount($userName, $userType)
72  {
73  $this->getConnection()->delete(
74  $this->getMainTable(),
75  ['user_name = ?' => $userName, 'user_type = ?' => $userType]
76  );
77  }
78 
82  public function incrementFailuresCount($userName, $userType)
83  {
84  $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
85  $date->add(new \DateInterval('PT' . $this->requestLogConfig->getLockTimeout() . 'S'));
86  $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
87 
88  $this->getConnection()->insertOnDuplicate(
89  $this->getMainTable(),
90  [
91  'user_name' => $userName,
92  'user_type' => $userType,
93  'failures_count' => 1,
94  'lock_expires_at' => $dateTime
95  ],
96  [
97  'failures_count' => new \Zend_Db_Expr('failures_count+1'),
98  'lock_expires_at' => new \Zend_Db_Expr("'" . $dateTime . "'")
99  ]
100  );
101  }
102 
106  public function clearExpiredFailures()
107  {
108  $date = (new \DateTime())->setTimestamp($this->dateTime->gmtTimestamp());
109  $dateTime = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
110  $this->getConnection()->delete($this->getMainTable(), ['lock_expires_at <= ?' => $dateTime]);
111  }
112 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $dateTime, RequestLogConfig $requestLogConfig, $connectionName=null)
Definition: RequestLog.php:37
$dateTime