NewsletterSubscriberSaveAfter.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Dotdigitalgroup\Email\Observer\Newsletter;
  3. /**
  4. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  5. */
  6. class NewsletterSubscriberSaveAfter implements \Magento\Framework\Event\ObserverInterface
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Helper\Config
  10. */
  11. private $configHelper;
  12. /**
  13. * @var \Magento\Framework\App\Response\RedirectInterface
  14. */
  15. private $redirect;
  16. /**
  17. * @var \Magento\Framework\HTTP\Header
  18. */
  19. private $header;
  20. /**
  21. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  22. */
  23. private $timezone;
  24. /**
  25. * @var \Magento\Framework\App\Request\Http
  26. */
  27. private $http;
  28. /**
  29. * @var \Dotdigitalgroup\Email\Model\ConsentFactory
  30. */
  31. private $consentFactory;
  32. /**
  33. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Consent
  34. */
  35. private $consentResource;
  36. /**
  37. * @var \Dotdigitalgroup\Email\Helper\Data
  38. */
  39. private $helper;
  40. /**
  41. * @var \Magento\Store\Model\StoreManagerInterface
  42. */
  43. private $storeManager;
  44. /**
  45. * @var \Dotdigitalgroup\Email\Model\ContactFactory
  46. */
  47. private $contactFactory;
  48. /**
  49. * NewsletterSubscriberSaveAfter constructor.
  50. * @param \Dotdigitalgroup\Email\Model\ConsentFactory $consentFactory
  51. * @param \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory
  52. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Consent $consentResource
  53. * @param \Dotdigitalgroup\Email\Helper\Data $data
  54. * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  55. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone
  56. * @param \Magento\Framework\App\Request\Http $http
  57. * @param \Magento\Framework\HTTP\Header $header
  58. * @param \Magento\Framework\App\Response\RedirectInterface $redirect
  59. */
  60. public function __construct(
  61. \Dotdigitalgroup\Email\Model\ConsentFactory $consentFactory,
  62. \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory,
  63. \Dotdigitalgroup\Email\Model\ResourceModel\Consent $consentResource,
  64. \Dotdigitalgroup\Email\Helper\Data $data,
  65. \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
  66. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone,
  67. \Magento\Framework\App\Request\Http $http,
  68. \Magento\Framework\HTTP\Header $header,
  69. \Magento\Framework\App\Response\RedirectInterface $redirect
  70. ) {
  71. $this->http = $http;
  72. $this->header = $header;
  73. $this->redirect = $redirect;
  74. $this->helper = $data;
  75. $this->configHelper = $this->helper->configHelperFactory->create();
  76. $this->timezone = $timezone;
  77. $this->storeManager = $storeManagerInterface;
  78. $this->consentFactory = $consentFactory;
  79. $this->contactFactory = $contactFactory;
  80. $this->consentResource = $consentResource;
  81. }
  82. /**
  83. * @param \Magento\Framework\Event\Observer $observer
  84. * @return $this
  85. */
  86. public function execute(\Magento\Framework\Event\Observer $observer)
  87. {
  88. $subscriber = $observer->getEvent()->getSubscriber();
  89. $email = $subscriber->getEmail();
  90. $subscriberStatus = $subscriber->getSubscriberStatus();
  91. $websiteId = $this->storeManager->getStore($subscriber->getStoreId())
  92. ->getWebsiteId();
  93. //If not confirmed or not enabled.
  94. if ($subscriberStatus == \Magento\Newsletter\Model\Subscriber::STATUS_UNSUBSCRIBED ||
  95. !$this->helper->isEnabled($websiteId) ||
  96. !$this->configHelper->isConsentSubscriberEnabled($websiteId)
  97. ) {
  98. return $this;
  99. }
  100. try {
  101. $contactEmail = $this->contactFactory->create()
  102. ->loadByCustomerEmail($email, $websiteId);
  103. $emailContactId = $contactEmail->getId();
  104. $consentModel = $this->consentFactory->create();
  105. $this->consentResource->load(
  106. $consentModel,
  107. $emailContactId,
  108. 'email_contact_id'
  109. );
  110. //don't update the consent data for guest subscribers or not confrimed
  111. if (! $consentModel->isObjectNew()) {
  112. return $this;
  113. }
  114. $consentIp = $this->http->getClientIp();
  115. $consentUrl = $this->redirect->getRefererUrl();
  116. $consentUserAgent = $this->header->getHttpUserAgent();
  117. //save the consent data against the contact
  118. $consentModel->setEmailContactId($emailContactId)
  119. ->setConsentUrl($consentUrl)
  120. ->setConsentDatetime(time())
  121. ->setConsentIp($consentIp)
  122. ->setConsentUserAgent($consentUserAgent);
  123. //save contact
  124. $this->consentResource->save($consentModel);
  125. } catch (\Exception $e) {
  126. $this->helper->debug((string)$e, []);
  127. }
  128. return $this;
  129. }
  130. }