Guest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Customer;
  3. /**
  4. * Guest sync cronjob.
  5. */
  6. class Guest
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Contact
  10. */
  11. private $contactResource;
  12. /**
  13. * @var int
  14. */
  15. private $countGuests = 0;
  16. /**
  17. * @var mixed
  18. */
  19. private $start;
  20. /**
  21. * @var \Dotdigitalgroup\Email\Helper\Data
  22. */
  23. private $helper;
  24. /**
  25. * @var \Dotdigitalgroup\Email\Helper\File
  26. */
  27. private $file;
  28. /**
  29. * @var \Dotdigitalgroup\Email\Model\ContactFactory
  30. */
  31. private $contactFactory;
  32. /**
  33. * @var \Dotdigitalgroup\Email\Model\ImporterFactory
  34. */
  35. private $importerFactory;
  36. /**
  37. * Guest constructor.
  38. * @param \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory
  39. * @param \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory
  40. * @param \Dotdigitalgroup\Email\Helper\File $file
  41. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Contact $contactResource
  42. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  43. */
  44. public function __construct(
  45. \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory,
  46. \Dotdigitalgroup\Email\Model\ContactFactory $contactFactory,
  47. \Dotdigitalgroup\Email\Helper\File $file,
  48. \Dotdigitalgroup\Email\Model\ResourceModel\Contact $contactResource,
  49. \Dotdigitalgroup\Email\Helper\Data $helper
  50. ) {
  51. $this->importerFactory = $importerFactory;
  52. $this->contactFactory = $contactFactory;
  53. $this->contactResource = $contactResource;
  54. $this->helper = $helper;
  55. $this->file = $file;
  56. }
  57. /**
  58. * GUEST SYNC.
  59. *
  60. * @return null
  61. */
  62. public function sync()
  63. {
  64. $this->start = microtime(true);
  65. $websites = $this->helper->getWebsites();
  66. foreach ($websites as $website) {
  67. //check if the guest is mapped and enabled
  68. $addresbook = $this->helper->getGuestAddressBook($website);
  69. $guestSyncEnabled = $this->helper->isGuestSyncEnabled($website);
  70. $apiEnabled = $this->helper->isEnabled($website);
  71. if ($addresbook && $guestSyncEnabled && $apiEnabled) {
  72. //sync guests for website
  73. $this->exportGuestPerWebsite($website);
  74. }
  75. }
  76. if ($this->countGuests) {
  77. $this->helper->log(
  78. '----------- Guest sync ----------- : ' .
  79. gmdate('H:i:s', microtime(true) - $this->start) .
  80. ', Total synced = ' . $this->countGuests
  81. );
  82. }
  83. }
  84. /**
  85. * Export guests for a website.
  86. *
  87. * @param \Magento\Store\Model\Website $website
  88. *
  89. * @return null
  90. * @throws \Magento\Framework\Exception\LocalizedException
  91. */
  92. public function exportGuestPerWebsite($website)
  93. {
  94. $onlySubscribers = $this->helper->isOnlySubscribersForContactSync($website->getWebsiteId());
  95. $contact = $this->contactFactory->create();
  96. $guests = ($onlySubscribers) ? $contact->getGuests($website, true) :
  97. $contact->getGuests($website);
  98. //found some guests
  99. if ($guests->getSize()) {
  100. $guestFilename = strtolower(
  101. $website->getCode() . '_guest_'
  102. . date('d_m_Y_His') . '.csv'
  103. );
  104. $this->helper->log('Guest file: ' . $guestFilename);
  105. $storeName = $this->helper->getMappedStoreName($website);
  106. $this->file->outputCSV(
  107. $this->file->getFilePath($guestFilename),
  108. ['Email', 'emailType', $storeName]
  109. );
  110. foreach ($guests as $guest) {
  111. $this->outputCsvToFile($guest, $website, $guestFilename);
  112. }
  113. if ($this->countGuests) {
  114. //register in queue with importer
  115. $this->importerFactory->create()
  116. ->registerQueue(
  117. \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_GUEST,
  118. '',
  119. \Dotdigitalgroup\Email\Model\Importer::MODE_BULK,
  120. $website->getId(),
  121. $guestFilename
  122. );
  123. }
  124. }
  125. }
  126. /**
  127. * Output
  128. *
  129. * @param \Dotdigitalgroup\Email\Model\Contact $guest
  130. * @param \Magento\Store\Model\Website $website
  131. * @param string $guestFilename
  132. *
  133. * @return null
  134. */
  135. private function outputCsvToFile($guest, $website, $guestFilename)
  136. {
  137. $email = $guest->getEmail();
  138. $guest->setEmailImported(\Dotdigitalgroup\Email\Model\Contact::EMAIL_CONTACT_IMPORTED);
  139. $this->contactResource->save($guest);
  140. $storeName = $website->getName();
  141. // save data for guests
  142. $this->file->outputCSV(
  143. $this->file->getFilePath($guestFilename),
  144. [$email, 'Html', $storeName]
  145. );
  146. ++$this->countGuests;
  147. }
  148. }