Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportConcurrentUsers.php
Go to the documentation of this file.
1 <?php
7 
11 
16 {
20  protected $config;
21 
25  protected $customerSession;
26 
31 
35  protected $storeManager;
36 
40  protected $usersFactory;
41 
45  protected $jsonEncoder;
46 
55  public function __construct(
57  \Magento\Customer\Model\Session $customerSession,
58  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
59  \Magento\Store\Model\StoreManagerInterface $storeManager,
60  \Magento\NewRelicReporting\Model\UsersFactory $usersFactory,
61  \Magento\Framework\Json\EncoderInterface $jsonEncoder
62  ) {
63  $this->config = $config;
64  $this->customerSession = $customerSession;
65  $this->customerRepository = $customerRepository;
66  $this->storeManager = $storeManager;
67  $this->usersFactory = $usersFactory;
68  $this->jsonEncoder = $jsonEncoder;
69  }
70 
78  public function execute(Observer $observer)
79  {
80  if ($this->config->isNewRelicEnabled()) {
81  if ($this->customerSession->isLoggedIn()) {
82  $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
83 
84  $jsonData = [
85  'id' => $customer->getId(),
86  'name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
87  'store' => $this->storeManager->getStore()->getName(),
88  'website' => $this->storeManager->getWebsite()->getName(),
89  ];
90 
91  $modelData = [
92  'type' => 'user_action',
93  'action' => $this->jsonEncoder->encode($jsonData),
94  ];
95 
97  $usersModel = $this->usersFactory->create();
98  $usersModel->setData($modelData);
99  $usersModel->save();
100  }
101  }
102  }
103 }
$customer
Definition: customers.php:11
__construct(Config $config, \Magento\Customer\Model\Session $customerSession, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\NewRelicReporting\Model\UsersFactory $usersFactory, \Magento\Framework\Json\EncoderInterface $jsonEncoder)