CustomerLoggedInObserver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Observer;
  7. use Magento\Customer\Api\GroupRepositoryInterface;
  8. use Magento\Customer\Model\Session;
  9. use Magento\Framework\Event\Observer;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\Framework\Module\Manager;
  12. use Magento\PageCache\Model\Config;
  13. use Magento\Tax\Api\TaxAddressManagerInterface;
  14. use Magento\Tax\Helper\Data;
  15. class CustomerLoggedInObserver implements ObserverInterface
  16. {
  17. /**
  18. * @var Session
  19. */
  20. protected $customerSession;
  21. /**
  22. * @var Data
  23. */
  24. protected $taxHelper;
  25. /**
  26. * Module manager
  27. *
  28. * @var Manager
  29. */
  30. private $moduleManager;
  31. /**
  32. * Cache config
  33. *
  34. * @var Config
  35. */
  36. private $cacheConfig;
  37. /**
  38. * @var GroupRepositoryInterface
  39. */
  40. private $groupRepository;
  41. /**
  42. * Manager to save data in customer session.
  43. *
  44. * @var TaxAddressManagerInterface
  45. */
  46. private $addressManager;
  47. /**
  48. * @param GroupRepositoryInterface $groupRepository
  49. * @param Session $customerSession
  50. * @param Data $taxHelper
  51. * @param Manager $moduleManager
  52. * @param Config $cacheConfig
  53. * @param TaxAddressManagerInterface $addressManager
  54. */
  55. public function __construct(
  56. GroupRepositoryInterface $groupRepository,
  57. Session $customerSession,
  58. Data $taxHelper,
  59. Manager $moduleManager,
  60. Config $cacheConfig,
  61. TaxAddressManagerInterface $addressManager
  62. ) {
  63. $this->groupRepository = $groupRepository;
  64. $this->customerSession = $customerSession;
  65. $this->taxHelper = $taxHelper;
  66. $this->moduleManager = $moduleManager;
  67. $this->cacheConfig = $cacheConfig;
  68. $this->addressManager = $addressManager;
  69. }
  70. /**
  71. * @param Observer $observer
  72. * @return void
  73. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  74. */
  75. public function execute(Observer $observer)
  76. {
  77. if ($this->moduleManager->isEnabled('Magento_PageCache')
  78. && $this->cacheConfig->isEnabled()
  79. && $this->taxHelper->isCatalogPriceDisplayAffectedByTax()
  80. ) {
  81. /** @var \Magento\Customer\Model\Data\Customer $customer */
  82. $customer = $observer->getData('customer');
  83. $customerGroupId = $customer->getGroupId();
  84. $customerGroup = $this->groupRepository->getById($customerGroupId);
  85. $customerTaxClassId = $customerGroup->getTaxClassId();
  86. $this->customerSession->setCustomerTaxClassId($customerTaxClassId);
  87. $addresses = $customer->getAddresses();
  88. if (isset($addresses)) {
  89. $this->addressManager->setDefaultAddressAfterLogIn($addresses);
  90. }
  91. }
  92. }
  93. }