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
8 
14 {
18  protected $date;
19 
23  protected $dateTime;
24 
31  public function __construct(
32  \Magento\Framework\Model\ResourceModel\Db\Context $context,
33  \Magento\Framework\Stdlib\DateTime\DateTime $date,
34  \Magento\Framework\Stdlib\DateTime $dateTime,
35  $connectionName = null
36  ) {
37  $this->date = $date;
38  $this->dateTime = $dateTime;
39  parent::__construct($context, $connectionName);
40  }
41 
47  protected function _construct()
48  {
49  $this->_init('customer_visitor', 'visitor_id');
50  }
51 
58  protected function _prepareDataForSave(\Magento\Framework\Model\AbstractModel $visitor)
59  {
60  return [
61  'customer_id' => $visitor->getCustomerId(),
62  'session_id' => $visitor->getSessionId(),
63  'last_visit_at' => $visitor->getLastVisitAt()
64  ];
65  }
66 
73  public function clean(\Magento\Customer\Model\Visitor $object)
74  {
75  $cleanTime = $object->getCleanTime();
76  $connection = $this->getConnection();
77  $timeLimit = $this->dateTime->formatDate($this->date->gmtTimestamp() - $cleanTime);
78  while (true) {
79  $select = $connection->select()->from(
80  ['visitor_table' => $this->getTable('customer_visitor')],
81  ['visitor_id' => 'visitor_table.visitor_id']
82  )->where(
83  'visitor_table.last_visit_at < ?',
84  $timeLimit
85  )->limit(
86  100
87  );
88  $visitorIds = $connection->fetchCol($select);
89  if (!$visitorIds) {
90  break;
91  }
92  $condition = ['visitor_id IN (?)' => $visitorIds];
93  $connection->delete($this->getTable('customer_visitor'), $condition);
94  }
95 
96  return $this;
97  }
98 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Framework\Stdlib\DateTime $dateTime, $connectionName=null)
Definition: Visitor.php:31
clean(\Magento\Customer\Model\Visitor $object)
Definition: Visitor.php:73
$connection
Definition: bulk.php:13
_prepareDataForSave(\Magento\Framework\Model\AbstractModel $visitor)
Definition: Visitor.php:58