ReviewSaveAutomation.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Dotdigitalgroup\Email\Observer\Customer;
  3. /**
  4. * New review automation.
  5. */
  6. class ReviewSaveAutomation implements \Magento\Framework\Event\ObserverInterface
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Review
  10. */
  11. private $reviewResource;
  12. /**
  13. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Automation
  14. */
  15. private $automationResource;
  16. /**
  17. * @var \Dotdigitalgroup\Email\Helper\Data
  18. */
  19. private $helper;
  20. /**
  21. * @var \Magento\Store\Model\StoreManagerInterface
  22. */
  23. private $storeManager;
  24. /**
  25. * @var \Magento\Customer\Model\CustomerFactory
  26. */
  27. private $customerFactory;
  28. /**
  29. * @var \Dotdigitalgroup\Email\Model\ReviewFactory
  30. */
  31. private $reviewFactory;
  32. /**
  33. * @var \Dotdigitalgroup\Email\Model\AutomationFactory
  34. */
  35. private $automationFactory;
  36. /**
  37. * @var \Magento\Customer\Model\ResourceModel\Customer
  38. */
  39. private $customerResource;
  40. /**
  41. * ReviewSaveAutomation constructor.
  42. *
  43. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Review $reviewResource
  44. * @param \Magento\Customer\Model\ResourceModel\Customer $customerResource
  45. * @param \Dotdigitalgroup\Email\Model\ReviewFactory $reviewFactory
  46. * @param \Dotdigitalgroup\Email\Model\AutomationFactory $automationFactory
  47. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Automation $automationResource
  48. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  49. * @param \Dotdigitalgroup\Email\Helper\Data $data
  50. * @param \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  51. */
  52. public function __construct(
  53. \Magento\Customer\Model\ResourceModel\Customer $customerResource,
  54. \Dotdigitalgroup\Email\Model\ReviewFactory $reviewFactory,
  55. \Dotdigitalgroup\Email\Model\ResourceModel\Review $reviewResource,
  56. \Dotdigitalgroup\Email\Model\AutomationFactory $automationFactory,
  57. \Dotdigitalgroup\Email\Model\ResourceModel\Automation $automationResource,
  58. \Magento\Customer\Model\CustomerFactory $customerFactory,
  59. \Dotdigitalgroup\Email\Helper\Data $data,
  60. \Magento\Store\Model\StoreManagerInterface $storeManagerInterface
  61. ) {
  62. $this->reviewFactory = $reviewFactory;
  63. $this->reviewResource = $reviewResource;
  64. $this->automationResource = $automationResource;
  65. $this->automationFactory = $automationFactory;
  66. $this->customerFactory = $customerFactory;
  67. $this->helper = $data;
  68. $this->storeManager = $storeManagerInterface;
  69. $this->customerResource = $customerResource;
  70. }
  71. /**
  72. * If it's configured to capture on shipment - do this.
  73. *
  74. * @param \Magento\Framework\Event\Observer $observer
  75. *
  76. * @return $this
  77. */
  78. public function execute(\Magento\Framework\Event\Observer $observer)
  79. {
  80. $dataObject = $observer->getEvent()->getDataObject();
  81. if ($dataObject->getCustomerId()
  82. && $dataObject->getStatusId()
  83. == \Magento\Review\Model\Review::STATUS_APPROVED
  84. ) {
  85. $customerId = $dataObject->getCustomerId();
  86. $this->helper->setConnectorContactToReImport($customerId);
  87. //save review info in the table
  88. $this->registerReview($dataObject);
  89. $store = $this->storeManager->getStore($dataObject->getStoreId());
  90. $storeName = $store->getName();
  91. $website = $this->storeManager->getStore($store)->getWebsite();
  92. $customer = $this->customerFactory->create();
  93. $this->customerResource->load($customer, $customerId);
  94. //if api is not enabled
  95. if (!$this->helper->isEnabled($website)) {
  96. return $this;
  97. }
  98. $programId
  99. = $this->helper->getWebsiteConfig('connector_automation/visitor_automation/review_automation');
  100. if ($programId) {
  101. $automation = $this->automationFactory->create();
  102. $automation->setEmail($customer->getEmail())
  103. ->setAutomationType(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_TYPE_NEW_REVIEW)
  104. ->setEnrolmentStatus(\Dotdigitalgroup\Email\Model\Sync\Automation::AUTOMATION_STATUS_PENDING)
  105. ->setTypeId($dataObject->getReviewId())
  106. ->setWebsiteId($website->getId())
  107. ->setStoreName($storeName)
  108. ->setProgramId($programId);
  109. $this->automationResource->save($automation);
  110. }
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Register review.
  116. *
  117. * @param mixed $review
  118. *
  119. * @return null
  120. */
  121. private function registerReview($review)
  122. {
  123. try {
  124. $reviewModel = $this->reviewFactory->create();
  125. $reviewModel->setReviewId($review->getReviewId())
  126. ->setCustomerId($review->getCustomerId())
  127. ->setStoreId($review->getStoreId());
  128. $this->reviewResource->save($reviewModel);
  129. } catch (\Exception $e) {
  130. $this->helper->debug((string)$e, []);
  131. }
  132. }
  133. }