CustomerNotification.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Plugin;
  7. use Magento\Customer\Api\CustomerRepositoryInterface;
  8. use Magento\Customer\Model\Customer\NotificationStorage;
  9. use Magento\Customer\Model\Session;
  10. use Magento\Framework\App\Action\AbstractAction;
  11. use Magento\Framework\App\Area;
  12. use Magento\Framework\App\RequestInterface;
  13. use Magento\Framework\App\State;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Psr\Log\LoggerInterface;
  16. class CustomerNotification
  17. {
  18. /**
  19. * @var Session
  20. */
  21. private $session;
  22. /**
  23. * @var NotificationStorage
  24. */
  25. private $notificationStorage;
  26. /**
  27. * @var CustomerRepositoryInterface
  28. */
  29. private $customerRepository;
  30. /**
  31. * @var State
  32. */
  33. private $state;
  34. /**
  35. * @var LoggerInterface
  36. */
  37. private $logger;
  38. /**
  39. * Initialize dependencies.
  40. *
  41. * @param Session $session
  42. * @param NotificationStorage $notificationStorage
  43. * @param State $state
  44. * @param CustomerRepositoryInterface $customerRepository
  45. * @param LoggerInterface $logger
  46. */
  47. public function __construct(
  48. Session $session,
  49. NotificationStorage $notificationStorage,
  50. State $state,
  51. CustomerRepositoryInterface $customerRepository,
  52. LoggerInterface $logger
  53. ) {
  54. $this->session = $session;
  55. $this->notificationStorage = $notificationStorage;
  56. $this->state = $state;
  57. $this->customerRepository = $customerRepository;
  58. $this->logger = $logger;
  59. }
  60. /**
  61. * @param AbstractAction $subject
  62. * @param RequestInterface $request
  63. * @return void
  64. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  65. */
  66. public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
  67. {
  68. $customerId = $this->session->getCustomerId();
  69. if ($this->state->getAreaCode() == Area::AREA_FRONTEND && $request->isPost()
  70. && $this->notificationStorage->isExists(
  71. NotificationStorage::UPDATE_CUSTOMER_SESSION,
  72. $customerId
  73. )
  74. ) {
  75. try {
  76. $customer = $this->customerRepository->getById($customerId);
  77. $this->session->setCustomerData($customer);
  78. $this->session->setCustomerGroupId($customer->getGroupId());
  79. $this->session->regenerateId();
  80. $this->notificationStorage->remove(NotificationStorage::UPDATE_CUSTOMER_SESSION, $customer->getId());
  81. } catch (NoSuchEntityException $e) {
  82. $this->logger->error($e);
  83. }
  84. }
  85. }
  86. }