Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Visitor.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Customer\Model;
8 
11 
18 {
19  const VISITOR_TYPE_CUSTOMER = 'c';
20 
21  const VISITOR_TYPE_VISITOR = 'v';
22 
24 
25  const XML_PATH_ONLINE_INTERVAL = 'customer/online_customers/online_minutes_interval';
26 
30  protected $ignoredUserAgents;
31 
35  protected $session;
36 
40  protected $httpHeader;
41 
45  protected $skipRequestLogging = false;
46 
52  protected $ignores;
53 
59  protected $scopeConfig;
60 
64  protected $dateTime;
65 
69  protected $indexerRegistry;
70 
74  private $requestSafety;
75 
92  public function __construct(
93  \Magento\Framework\Model\Context $context,
94  \Magento\Framework\Registry $registry,
96  \Magento\Framework\HTTP\Header $httpHeader,
98  \Magento\Framework\Stdlib\DateTime $dateTime,
99  \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
100  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
101  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
102  array $ignoredUserAgents = [],
103  array $ignores = [],
104  array $data = [],
105  RequestSafetyInterface $requestSafety = null
106  ) {
107  $this->session = $session;
108  $this->httpHeader = $httpHeader;
109  $this->ignoredUserAgents = $ignoredUserAgents;
110  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
111  $this->ignores = $ignores;
112  $this->scopeConfig = $scopeConfig;
113  $this->dateTime = $dateTime;
114  $this->indexerRegistry = $indexerRegistry;
115  $this->requestSafety = $requestSafety ?? ObjectManager::getInstance()->get(RequestSafetyInterface::class);
116  }
117 
123  protected function _construct()
124  {
125  $this->_init(\Magento\Customer\Model\ResourceModel\Visitor::class);
126  $userAgent = $this->httpHeader->getHttpUserAgent();
127  if ($this->ignoredUserAgents) {
128  if (in_array($userAgent, $this->ignoredUserAgents)) {
129  $this->skipRequestLogging = true;
130  }
131  }
132  }
133 
141  {
142  $this->skipRequestLogging = (bool)$skipRequestLogging;
143  return $this;
144  }
145 
154  public function initByRequest($observer)
155  {
156  if ($this->skipRequestLogging || $this->isModuleIgnored($observer)) {
157  return $this;
158  }
159 
160  if ($this->session->getVisitorData()) {
161  $this->setData($this->session->getVisitorData());
162  if ($this->getSessionId() != $this->session->getSessionId()) {
163  $this->setSessionId($this->session->getSessionId());
164  }
165  }
166 
167  $this->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
168 
169  // prevent saving Visitor for safe methods, e.g. GET request
170  if ($this->requestSafety->isSafeMethod()) {
171  return $this;
172  }
173  if (!$this->getId()) {
174  $this->setSessionId($this->session->getSessionId());
175  $this->save();
176  $this->_eventManager->dispatch('visitor_init', ['visitor' => $this]);
177  $this->session->setVisitorData($this->getData());
178  }
179  return $this;
180  }
181 
190  public function saveByRequest($observer)
191  {
192  // prevent saving Visitor for safe methods, e.g. GET request
193  if ($this->skipRequestLogging || $this->requestSafety->isSafeMethod() || $this->isModuleIgnored($observer)) {
194  return $this;
195  }
196 
197  try {
198  $this->save();
199  $this->_eventManager->dispatch('visitor_activity_save', ['visitor' => $this]);
200  $this->session->setVisitorData($this->getData());
201  } catch (\Exception $e) {
202  $this->_logger->critical($e);
203  }
204  return $this;
205  }
206 
213  public function isModuleIgnored($observer)
214  {
215  if (is_array($this->ignores) && $observer) {
216  $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
217  if (isset($this->ignores[$curModule])) {
218  return true;
219  }
220  }
221  return false;
222  }
223 
232  public function bindCustomerLogin($observer)
233  {
235  $customer = $observer->getEvent()->getCustomer();
236  if (!$this->getCustomerId()) {
237  $this->setDoCustomerLogin(true);
238  $this->setCustomerId($customer->getId());
239  }
240  return $this;
241  }
242 
252  public function bindCustomerLogout($observer)
253  {
254  if ($this->getCustomerId()) {
255  $this->setDoCustomerLogout(true);
256  }
257  return $this;
258  }
259 
266  public function bindQuoteCreate($observer)
267  {
268  $quote = $observer->getEvent()->getQuote();
269  if ($quote) {
270  if ($quote->getIsCheckoutCart()) {
271  $this->setQuoteId($quote->getId());
272  $this->setDoQuoteCreate(true);
273  }
274  }
275  return $this;
276  }
277 
283  public function bindQuoteDestroy($observer)
284  {
285  $quote = $observer->getEvent()->getQuote();
286  if ($quote) {
287  $this->setDoQuoteDestroy(true);
288  }
289  return $this;
290  }
291 
297  public function getCleanTime()
298  {
299  return $this->scopeConfig->getValue(
300  \Magento\Framework\Session\Config::XML_PATH_COOKIE_LIFETIME,
301  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
302  ) + 86400;
303  }
304 
310  public function clean()
311  {
312  $this->getResource()->clean($this);
313  return $this;
314  }
315 
321  public function getOnlineInterval()
322  {
323  $configValue = intval(
324  $this->scopeConfig->getValue(
325  static::XML_PATH_ONLINE_INTERVAL,
326  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
327  )
328  );
329  return $configValue ?: static::DEFAULT_ONLINE_MINUTES_INTERVAL;
330  }
331 }
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\HTTP\Header $httpHeader, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $ignoredUserAgents=[], array $ignores=[], array $data=[], RequestSafetyInterface $requestSafety=null)
Definition: Visitor.php:92
getData($key='', $index=null)
Definition: DataObject.php:119
$customer
Definition: customers.php:11
$quote
$resource
Definition: bulk.php:12
setSkipRequestLogging($skipRequestLogging)
Definition: Visitor.php:140