PendingContactUpdater.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\AbandonedCart;
  3. use Dotdigitalgroup\Email\Model\Sales\Quote;
  4. use Dotdigitalgroup\Email\Model\Sync\Automation;
  5. class PendingContactUpdater
  6. {
  7. /**
  8. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned\CollectionFactory
  9. */
  10. private $abandonedCollectionFactory;
  11. /**
  12. * @var \Dotdigitalgroup\Email\Helper\Data
  13. */
  14. private $helper;
  15. /**
  16. * @var \Dotdigitalgroup\Email\Model\DateIntervalFactory
  17. */
  18. private $dateIntervalFactory;
  19. /**
  20. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  21. */
  22. private $timeZone;
  23. /**
  24. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned
  25. */
  26. private $abandonedResource;
  27. /**
  28. * @var \Magento\Framework\Stdlib\DateTime
  29. */
  30. private $dateTime;
  31. /**
  32. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned\CollectionFactory $abandonedCollectionFactory
  33. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  34. * @param \Dotdigitalgroup\Email\Model\DateIntervalFactory $dateIntervalFactory
  35. * @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timeZone
  36. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned $abandonedResource
  37. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  38. */
  39. public function __construct(
  40. \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned\CollectionFactory $abandonedCollectionFactory,
  41. \Dotdigitalgroup\Email\Helper\Data $helper,
  42. \Dotdigitalgroup\Email\Model\DateIntervalFactory $dateIntervalFactory,
  43. \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timeZone,
  44. \Dotdigitalgroup\Email\Model\ResourceModel\Abandoned $abandonedResource,
  45. \Magento\Framework\Stdlib\DateTime $dateTime
  46. ) {
  47. $this->abandonedCollectionFactory = $abandonedCollectionFactory;
  48. $this->helper = $helper;
  49. $this->dateIntervalFactory = $dateIntervalFactory;
  50. $this->timeZone = $timeZone;
  51. $this->abandonedResource = $abandonedResource;
  52. $this->dateTime = $dateTime;
  53. }
  54. /**
  55. * @return void
  56. *
  57. * @throws \Magento\Framework\Exception\NoSuchEntityException
  58. */
  59. public function update()
  60. {
  61. if ($this->itIsTimeToCheckPendingContact()) {
  62. $this->checkStatusForPendingContactsInternal();
  63. }
  64. }
  65. /**
  66. * @return boolean
  67. */
  68. private function itIsTimeToCheckPendingContact()
  69. {
  70. $dateTimeFromDb = $this->abandonedCollectionFactory->create()->getLastPendingStatusCheckTime();
  71. if (!$dateTimeFromDb) {
  72. return false;
  73. }
  74. $lastCheckTime = $this->timeZone->date($dateTimeFromDb);
  75. $interval = $this->dateIntervalFactory->create(['interval_spec' => 'PT30M']);
  76. $lastCheckTime->add($interval);
  77. $now = $this->timeZone->date();
  78. return ($now->format('Y-m-d H:i:s') > $lastCheckTime->format('Y-m-d H:i:s'));
  79. }
  80. /**
  81. * @throws \Magento\Framework\Exception\NoSuchEntityException
  82. */
  83. private function checkStatusForPendingContactsInternal()
  84. {
  85. $updatedAt = $this->dateTime->formatDate(true);
  86. $expiryDate = $this->getDateTimeForExpiration();
  87. $collection = $this->abandonedCollectionFactory->create()
  88. ->getCollectionByPendingStatus();
  89. $idsToUpdateStatus = [];
  90. $idsToUpdateDate = [];
  91. $idsToExpire = [];
  92. foreach ($collection as $item) {
  93. $websiteId = $this->helper->storeManager->getStore($item->getStoreId())->getWebsiteId();
  94. $contact = $this->helper->getContact($item->getEmail(), $websiteId);
  95. if (isset($contact->id) && $contact->status !== Automation::CONTACT_STATUS_PENDING) {
  96. $idsToUpdateStatus[] = $item->getId();
  97. } elseif (($item->getCreatedAt() < $expiryDate) &&
  98. $contact->status === Automation::CONTACT_STATUS_PENDING
  99. ) {
  100. $idsToExpire[] = $item->getId();
  101. } else {
  102. $idsToUpdateDate[] = $item->getId();
  103. }
  104. }
  105. $this->updateCarts($idsToUpdateStatus, $idsToUpdateDate, $idsToExpire, $updatedAt);
  106. }
  107. /**
  108. * @return string
  109. */
  110. private function getDateTimeForExpiration()
  111. {
  112. $hours = $this->helper->getWebsiteConfig(
  113. \Dotdigitalgroup\Email\Helper\Config::XML_PATH_CONNECTOR_AC_AUTOMATION_EXPIRE_TIME
  114. );
  115. $interval = $this->dateIntervalFactory->create(
  116. ['interval_spec' => sprintf('PT%sH', $hours)]
  117. );
  118. $dateTime = $this->timeZone->date();
  119. $dateTime->sub($interval);
  120. return $dateTime->format('Y-m-d H:i:s');
  121. }
  122. /**
  123. * @param int[] $idsToUpdateStatus
  124. * @param int[] $idsToUpdateDate
  125. * @param int[] $idsToExpire
  126. * @param string $updatedAt
  127. */
  128. private function updateCarts($idsToUpdateStatus, $idsToUpdateDate, $idsToExpire, $updatedAt)
  129. {
  130. $this->abandonedResource
  131. ->update(
  132. $idsToUpdateStatus,
  133. $updatedAt,
  134. Quote::STATUS_CONFIRMED
  135. );
  136. $this->abandonedResource
  137. ->update(
  138. $idsToUpdateDate,
  139. $updatedAt,
  140. Quote::STATUS_PENDING
  141. );
  142. $this->abandonedResource
  143. ->update(
  144. $idsToExpire,
  145. $updatedAt,
  146. Quote::STATUS_EXPIRED
  147. );
  148. }
  149. }