Visitor.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\App\RequestSafetyInterface;
  9. /**
  10. * Class Visitor
  11. *
  12. * @package Magento\Customer\Model
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
  15. */
  16. class Visitor extends \Magento\Framework\Model\AbstractModel
  17. {
  18. const VISITOR_TYPE_CUSTOMER = 'c';
  19. const VISITOR_TYPE_VISITOR = 'v';
  20. const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
  21. const XML_PATH_ONLINE_INTERVAL = 'customer/online_customers/online_minutes_interval';
  22. /**
  23. * @var string[]
  24. */
  25. protected $ignoredUserAgents;
  26. /**
  27. * @var \Magento\Framework\Session\SessionManagerInterface
  28. */
  29. protected $session;
  30. /**
  31. * @var \Magento\Framework\HTTP\Header
  32. */
  33. protected $httpHeader;
  34. /**
  35. * @var bool
  36. */
  37. protected $skipRequestLogging = false;
  38. /**
  39. * Ignored Modules
  40. *
  41. * @var array
  42. */
  43. protected $ignores;
  44. /**
  45. * Core store config
  46. *
  47. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  48. */
  49. protected $scopeConfig;
  50. /**
  51. * @var \Magento\Framework\Stdlib\DateTime
  52. */
  53. protected $dateTime;
  54. /**
  55. * @var \Magento\Framework\Indexer\IndexerRegistry
  56. */
  57. protected $indexerRegistry;
  58. /**
  59. * @var RequestSafetyInterface
  60. */
  61. private $requestSafety;
  62. /**
  63. * @param \Magento\Framework\Model\Context $context
  64. * @param \Magento\Framework\Registry $registry
  65. * @param \Magento\Framework\Session\SessionManagerInterface $session
  66. * @param \Magento\Framework\HTTP\Header $httpHeader
  67. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  68. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  69. * @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
  70. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  71. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  72. * @param array $ignoredUserAgents
  73. * @param array $ignores
  74. * @param array $data
  75. * @param RequestSafetyInterface|null $requestSafety
  76. *
  77. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  78. */
  79. public function __construct(
  80. \Magento\Framework\Model\Context $context,
  81. \Magento\Framework\Registry $registry,
  82. \Magento\Framework\Session\SessionManagerInterface $session,
  83. \Magento\Framework\HTTP\Header $httpHeader,
  84. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  85. \Magento\Framework\Stdlib\DateTime $dateTime,
  86. \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
  87. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  88. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  89. array $ignoredUserAgents = [],
  90. array $ignores = [],
  91. array $data = [],
  92. RequestSafetyInterface $requestSafety = null
  93. ) {
  94. $this->session = $session;
  95. $this->httpHeader = $httpHeader;
  96. $this->ignoredUserAgents = $ignoredUserAgents;
  97. parent::__construct($context, $registry, $resource, $resourceCollection, $data);
  98. $this->ignores = $ignores;
  99. $this->scopeConfig = $scopeConfig;
  100. $this->dateTime = $dateTime;
  101. $this->indexerRegistry = $indexerRegistry;
  102. $this->requestSafety = $requestSafety ?? ObjectManager::getInstance()->get(RequestSafetyInterface::class);
  103. }
  104. /**
  105. * Object initialization
  106. *
  107. * @return void
  108. */
  109. protected function _construct()
  110. {
  111. $this->_init(\Magento\Customer\Model\ResourceModel\Visitor::class);
  112. $userAgent = $this->httpHeader->getHttpUserAgent();
  113. if ($this->ignoredUserAgents) {
  114. if (in_array($userAgent, $this->ignoredUserAgents)) {
  115. $this->skipRequestLogging = true;
  116. }
  117. }
  118. }
  119. /**
  120. * Skip request logging
  121. *
  122. * @param bool $skipRequestLogging
  123. * @return \Magento\Customer\Model\Visitor
  124. */
  125. public function setSkipRequestLogging($skipRequestLogging)
  126. {
  127. $this->skipRequestLogging = (bool)$skipRequestLogging;
  128. return $this;
  129. }
  130. /**
  131. * Initialization visitor by request
  132. *
  133. * Used in event "controller_action_predispatch"
  134. *
  135. * @param \Magento\Framework\Event\Observer $observer
  136. * @return \Magento\Customer\Model\Visitor
  137. */
  138. public function initByRequest($observer)
  139. {
  140. if ($this->skipRequestLogging || $this->isModuleIgnored($observer)) {
  141. return $this;
  142. }
  143. if ($this->session->getVisitorData()) {
  144. $this->setData($this->session->getVisitorData());
  145. if ($this->getSessionId() != $this->session->getSessionId()) {
  146. $this->setSessionId($this->session->getSessionId());
  147. }
  148. }
  149. $this->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
  150. if (!$this->getId()) {
  151. $this->setSessionId($this->session->getSessionId());
  152. $this->save();
  153. $this->_eventManager->dispatch('visitor_init', ['visitor' => $this]);
  154. $this->session->setVisitorData($this->getData());
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Save visitor by request
  160. *
  161. * Used in event "controller_action_postdispatch"
  162. *
  163. * @param \Magento\Framework\Event\Observer $observer
  164. * @return \Magento\Customer\Model\Visitor
  165. */
  166. public function saveByRequest($observer)
  167. {
  168. // prevent saving Visitor for safe methods, e.g. GET request
  169. if ($this->skipRequestLogging || $this->requestSafety->isSafeMethod() || $this->isModuleIgnored($observer)) {
  170. return $this;
  171. }
  172. try {
  173. $this->save();
  174. $this->_eventManager->dispatch('visitor_activity_save', ['visitor' => $this]);
  175. $this->session->setVisitorData($this->getData());
  176. } catch (\Exception $e) {
  177. $this->_logger->critical($e);
  178. }
  179. return $this;
  180. }
  181. /**
  182. * Returns true if the module is required
  183. *
  184. * @param \Magento\Framework\Event\Observer $observer
  185. * @return bool
  186. */
  187. public function isModuleIgnored($observer)
  188. {
  189. if (is_array($this->ignores) && $observer) {
  190. $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
  191. if (isset($this->ignores[$curModule])) {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * Bind customer data when customer login
  199. *
  200. * Used in event "customer_login"
  201. *
  202. * @param \Magento\Framework\Event\Observer $observer
  203. * @return \Magento\Customer\Model\Visitor
  204. */
  205. public function bindCustomerLogin($observer)
  206. {
  207. /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
  208. $customer = $observer->getEvent()->getCustomer();
  209. if (!$this->getCustomerId()) {
  210. $this->setDoCustomerLogin(true);
  211. $this->setCustomerId($customer->getId());
  212. }
  213. return $this;
  214. }
  215. /**
  216. * Bind customer data when customer logout
  217. *
  218. * Used in event "customer_logout"
  219. *
  220. * @param \Magento\Framework\Event\Observer $observer
  221. * @return \Magento\Customer\Model\Visitor
  222. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  223. */
  224. public function bindCustomerLogout($observer)
  225. {
  226. if ($this->getCustomerId()) {
  227. $this->setDoCustomerLogout(true);
  228. }
  229. return $this;
  230. }
  231. /**
  232. * Create binding of checkout quote
  233. *
  234. * @param \Magento\Framework\Event\Observer $observer
  235. * @return \Magento\Customer\Model\Visitor
  236. */
  237. public function bindQuoteCreate($observer)
  238. {
  239. $quote = $observer->getEvent()->getQuote();
  240. if ($quote) {
  241. if ($quote->getIsCheckoutCart()) {
  242. $this->setQuoteId($quote->getId());
  243. $this->setDoQuoteCreate(true);
  244. }
  245. }
  246. return $this;
  247. }
  248. /**
  249. * Destroy binding of checkout quote
  250. *
  251. * @param \Magento\Framework\Event\Observer $observer
  252. * @return \Magento\Customer\Model\Visitor
  253. */
  254. public function bindQuoteDestroy($observer)
  255. {
  256. $quote = $observer->getEvent()->getQuote();
  257. if ($quote) {
  258. $this->setDoQuoteDestroy(true);
  259. }
  260. return $this;
  261. }
  262. /**
  263. * Return clean time in seconds for visitor's outdated records
  264. *
  265. * @return string
  266. */
  267. public function getCleanTime()
  268. {
  269. return $this->scopeConfig->getValue(
  270. \Magento\Framework\Session\Config::XML_PATH_COOKIE_LIFETIME,
  271. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  272. ) + 86400;
  273. }
  274. /**
  275. * Clean visitor's outdated records
  276. *
  277. * @return $this
  278. */
  279. public function clean()
  280. {
  281. $this->getResource()->clean($this);
  282. return $this;
  283. }
  284. /**
  285. * Retrieve Online Interval (in minutes)
  286. *
  287. * @return int Minutes Interval
  288. */
  289. public function getOnlineInterval()
  290. {
  291. $configValue = (int)$this->scopeConfig->getValue(
  292. static::XML_PATH_ONLINE_INTERVAL,
  293. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  294. );
  295. return $configValue ?: static::DEFAULT_ONLINE_MINUTES_INTERVAL;
  296. }
  297. }