Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EventSaver.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_storeManager;
17 
21  protected $_eventFactory;
22 
26  protected $_customerSession;
27 
31  protected $_customerVisitor;
32 
39  public function __construct(
40  \Magento\Store\Model\StoreManagerInterface $storeManager,
41  \Magento\Reports\Model\EventFactory $event,
42  \Magento\Customer\Model\Session $customerSession,
43  \Magento\Customer\Model\Visitor $customerVisitor
44  ) {
45  $this->_storeManager = $storeManager;
46  $this->_eventFactory = $event;
47  $this->_customerSession = $customerSession;
48  $this->_customerVisitor = $customerVisitor;
49  }
50 
60  public function save($eventTypeId, $objectId, $subjectId = null, $subtype = 0)
61  {
62  if ($subjectId === null) {
63  if ($this->_customerSession->isLoggedIn()) {
64  $subjectId = $this->_customerSession->getCustomerId();
65  } else {
66  $subjectId = $this->_customerVisitor->getId();
67  $subtype = 1;
68  }
69  }
70 
72  $eventModel = $this->_eventFactory->create();
73  $storeId = $this->_storeManager->getStore()->getId();
74  $eventModel->setData([
75  'event_type_id' => $eventTypeId,
76  'object_id' => $objectId,
77  'subject_id' => $subjectId,
78  'subtype' => $subtype,
79  'store_id' => $storeId,
80  ]);
81 
82  $eventModel->save();
83  }
84 }
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Reports\Model\EventFactory $event, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Model\Visitor $customerVisitor)
Definition: EventSaver.php:39
$storeManager