CustomerLoginObserver.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. /**
  9. * Reports Event observer model
  10. */
  11. class CustomerLoginObserver implements ObserverInterface
  12. {
  13. /**
  14. * @var \Magento\Reports\Model\EventFactory
  15. */
  16. protected $_eventFactory;
  17. /**
  18. * @var \Magento\Reports\Model\Product\Index\ComparedFactory
  19. */
  20. protected $_productCompFactory;
  21. /**
  22. * @var \Magento\Reports\Model\Product\Index\ViewedFactory
  23. */
  24. protected $_productIndexFactory;
  25. /**
  26. * @var \Magento\Customer\Model\Session
  27. */
  28. protected $_customerSession;
  29. /**
  30. * @var \Magento\Customer\Model\Visitor
  31. */
  32. protected $_customerVisitor;
  33. /**
  34. * @param \Magento\Reports\Model\EventFactory $event
  35. * @param \Magento\Reports\Model\Product\Index\ComparedFactory $productCompFactory
  36. * @param \Magento\Reports\Model\Product\Index\ViewedFactory $productIndexFactory
  37. * @param \Magento\Customer\Model\Session $customerSession
  38. * @param \Magento\Customer\Model\Visitor $customerVisitor
  39. */
  40. public function __construct(
  41. \Magento\Reports\Model\EventFactory $event,
  42. \Magento\Reports\Model\Product\Index\ComparedFactory $productCompFactory,
  43. \Magento\Reports\Model\Product\Index\ViewedFactory $productIndexFactory,
  44. \Magento\Customer\Model\Session $customerSession,
  45. \Magento\Customer\Model\Visitor $customerVisitor
  46. ) {
  47. $this->_eventFactory = $event;
  48. $this->_productCompFactory = $productCompFactory;
  49. $this->_productIndexFactory = $productIndexFactory;
  50. $this->_customerSession = $customerSession;
  51. $this->_customerVisitor = $customerVisitor;
  52. }
  53. /**
  54. * Customer login action
  55. *
  56. * @param \Magento\Framework\Event\Observer $observer
  57. * @return $this
  58. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  59. */
  60. public function execute(\Magento\Framework\Event\Observer $observer)
  61. {
  62. if (!$this->_customerSession->isLoggedIn()) {
  63. return $this;
  64. }
  65. $visitorId = $this->_customerVisitor->getId();
  66. $customerId = $this->_customerSession->getCustomerId();
  67. $eventModel = $this->_eventFactory->create();
  68. $eventModel->updateCustomerType($visitorId, $customerId);
  69. $this->_productCompFactory->create()->updateCustomerFromVisitor()->calculate();
  70. $this->_productIndexFactory->create()->updateCustomerFromVisitor()->calculate();
  71. return $this;
  72. }
  73. }