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 declare(strict_types=1);
7 
9 
11 use Magento\ReleaseNotification\Model\Viewer\LogFactory;
13 
19 class Logger
20 {
24  const LOG_TABLE_NAME = 'release_notification_viewer_log';
25 
29  private $resource;
30 
34  private $logFactory;
35 
41  public function __construct(
42  ResourceConnection $resource,
43  LogFactory $logFactory
44  ) {
45  $this->resource = $resource;
46  $this->logFactory = $logFactory;
47  }
48 
56  public function log(int $viewerId, string $lastViewVersion) : bool
57  {
59  $connection = $this->resource->getConnection(ResourceConnection::DEFAULT_CONNECTION);
60  $connection->insertOnDuplicate(
61  $this->resource->getTableName(self::LOG_TABLE_NAME),
62  [
63  'viewer_id' => $viewerId,
64  'last_view_version' => $lastViewVersion
65  ],
66  [
67  'last_view_version'
68  ]
69  );
70  return true;
71  }
72 
79  public function get(int $viewerId) : Log
80  {
81  return $this->logFactory->create(['data' => $this->loadLogData($viewerId)]);
82  }
83 
90  private function loadLogData(int $viewerId) : array
91  {
92  $connection = $this->resource->getConnection();
93  $select = $connection->select()
94  ->from($this->resource->getTableName(self::LOG_TABLE_NAME))
95  ->where('viewer_id = ?', $viewerId);
96 
97  $data = $connection->fetchRow($select);
98  if (!$data) {
99  $data = [];
100  }
101  return $data;
102  }
103 }
__construct(ResourceConnection $resource, LogFactory $logFactory)
Definition: Logger.php:41
$resource
Definition: bulk.php:12
$connection
Definition: bulk.php:13