Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Logger.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
9 
15 class Logger
16 {
22  protected $resource;
23 
27  protected $logFactory;
28 
33  public function __construct(
35  \Magento\Customer\Model\LogFactory $logFactory
36  ) {
37  $this->resource = $resource;
38  $this->logFactory = $logFactory;
39  }
40 
49  public function log($customerId, array $data)
50  {
51  $data = array_filter($data);
52 
53  if (!$data) {
54  throw new \InvalidArgumentException("Log data is empty");
55  }
56 
58  $connection = $this->resource->getConnection(ResourceConnection::DEFAULT_CONNECTION);
59 
60  $connection->insertOnDuplicate(
61  $this->resource->getTableName('customer_log'),
62  array_merge(['customer_id' => $customerId], $data),
63  array_keys($data)
64  );
65 
66  return $this;
67  }
68 
75  public function get($customerId = null)
76  {
77  $data = (null !== $customerId) ? $this->loadLogData($customerId) : [];
78 
79  return $this->logFactory->create(
80  [
81  'customerId' => isset($data['customer_id']) ? $data['customer_id'] : null,
82  'lastLoginAt' => isset($data['last_login_at']) ? $data['last_login_at'] : null,
83  'lastLogoutAt' => isset($data['last_logout_at']) ? $data['last_logout_at'] : null,
84  'lastVisitAt' => isset($data['last_visit_at']) ? $data['last_visit_at'] : null
85  ]
86  );
87  }
88 
95  protected function loadLogData($customerId)
96  {
98  $connection = $this->resource->getConnection();
99 
100  $select = $connection->select()
101  ->from(
102  ['cl' => $this->resource->getTableName('customer_log')]
103  )
104  ->joinLeft(
105  ['cv' => $this->resource->getTableName('customer_visitor')],
106  'cv.customer_id = cl.customer_id',
107  ['last_visit_at']
108  )
109  ->where(
110  'cl.customer_id = ?',
112  )
113  ->order(
114  'cv.visitor_id DESC'
115  )
116  ->limit(1);
117 
118  return $connection->fetchRow($select);
119  }
120 }
__construct(ResourceConnection $resource, \Magento\Customer\Model\LogFactory $logFactory)
Definition: Logger.php:33
$connection
Definition: bulk.php:13