Cron.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model;
  3. use Dotdigitalgroup\Email\Setup\Schema;
  4. /**
  5. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  6. */
  7. class Cron
  8. {
  9. /**
  10. * @var Email\TemplateFactory
  11. */
  12. private $templateFactory;
  13. /**
  14. * @var Apiconnector\ContactFactory
  15. */
  16. private $contactFactory;
  17. /**
  18. * @var Sync\AutomationFactory
  19. */
  20. private $automationFactory;
  21. /**
  22. * @var ImporterFactory
  23. */
  24. private $importerFactory;
  25. /**
  26. * @var Sync\CatalogFactory
  27. */
  28. private $catalogFactory;
  29. /**
  30. * @var Newsletter\SubscriberFactory
  31. */
  32. private $subscriberFactory;
  33. /**
  34. * @var Customer\GuestFactory
  35. */
  36. private $guestFactory;
  37. /**
  38. * @var Sales\QuoteFactory
  39. */
  40. private $quoteFactory;
  41. /**
  42. * @var Sync\OrderFactory
  43. */
  44. private $syncOrderFactory;
  45. /**
  46. * @var Sync\CampaignFactory
  47. */
  48. private $campaignFactory;
  49. /**
  50. * @var \Dotdigitalgroup\Email\Helper\Data
  51. */
  52. private $helper;
  53. /**
  54. * @var \Dotdigitalgroup\Email\Helper\File
  55. */
  56. private $fileHelper;
  57. /**
  58. * @var ResourceModel\Importer
  59. */
  60. private $importerResource;
  61. /**
  62. * @var ResourceModel\Cron\CollectionFactory
  63. */
  64. private $cronCollection;
  65. /**
  66. * @var Cron\CronSub
  67. */
  68. private $cronHelper;
  69. /**
  70. * Cron constructor.
  71. *
  72. * @param Sync\CampaignFactory $campaignFactory
  73. * @param Sync\OrderFactory $syncOrderFactory
  74. * @param Sales\QuoteFactory $quoteFactory
  75. * @param Customer\GuestFactory $guestFactory
  76. * @param Newsletter\SubscriberFactory $subscriberFactory
  77. * @param Sync\CatalogFactory $catalogFactorty
  78. * @param ImporterFactory $importerFactory
  79. * @param Sync\AutomationFactory $automationFactory
  80. * @param Apiconnector\ContactFactory $contact
  81. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  82. * @param \Dotdigitalgroup\Email\Helper\File $fileHelper
  83. * @param ResourceModel\Importer $importerResource
  84. * @param ResourceModel\Cron\CollectionFactory $cronCollection
  85. * @param Cron\CronSubFactory $cronSubFactory
  86. *
  87. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  88. */
  89. public function __construct(
  90. \Dotdigitalgroup\Email\Model\Sync\CampaignFactory $campaignFactory,
  91. \Dotdigitalgroup\Email\Model\Sync\OrderFactory $syncOrderFactory,
  92. \Dotdigitalgroup\Email\Model\Sales\QuoteFactory $quoteFactory,
  93. \Dotdigitalgroup\Email\Model\Customer\GuestFactory $guestFactory,
  94. \Dotdigitalgroup\Email\Model\Newsletter\SubscriberFactory $subscriberFactory,
  95. \Dotdigitalgroup\Email\Model\Sync\CatalogFactory $catalogFactorty,
  96. \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory,
  97. \Dotdigitalgroup\Email\Model\Sync\AutomationFactory $automationFactory,
  98. \Dotdigitalgroup\Email\Model\Apiconnector\ContactFactory $contact,
  99. \Dotdigitalgroup\Email\Helper\Data $helper,
  100. \Dotdigitalgroup\Email\Helper\File $fileHelper,
  101. \Dotdigitalgroup\Email\Model\ResourceModel\Importer $importerResource,
  102. \Dotdigitalgroup\Email\Model\Email\TemplateFactory $templateFactory,
  103. \Dotdigitalgroup\Email\Model\ResourceModel\Cron\CollectionFactory $cronCollection,
  104. Cron\CronSubFactory $cronSubFactory
  105. ) {
  106. $this->campaignFactory = $campaignFactory;
  107. $this->syncOrderFactory = $syncOrderFactory;
  108. $this->quoteFactory = $quoteFactory;
  109. $this->guestFactory = $guestFactory;
  110. $this->subscriberFactory = $subscriberFactory;
  111. $this->catalogFactory = $catalogFactorty;
  112. $this->importerFactory = $importerFactory;
  113. $this->automationFactory = $automationFactory;
  114. $this->contactFactory = $contact;
  115. $this->helper = $helper;
  116. $this->fileHelper = $fileHelper;
  117. $this->importerResource = $importerResource;
  118. $this->cronCollection = $cronCollection;
  119. $this->templateFactory = $templateFactory;
  120. $this->cronHelper = $cronSubFactory->create();
  121. }
  122. /**
  123. * CRON FOR CONTACTS SYNC.
  124. *
  125. * @return array
  126. */
  127. public function contactSync()
  128. {
  129. if ($this->jobHasAlreadyBeenRun('ddg_automation_customer_subscriber_guest_sync')) {
  130. $message = 'Skipping ddg_automation_customer_subscriber_guest_sync job run';
  131. $this->helper->log($message);
  132. return ['message' => $message];
  133. }
  134. //run the sync for contacts
  135. $result = $this->contactFactory->create()
  136. ->sync();
  137. //run subscribers and guests sync
  138. $subscriberResult = $this->subscribersAndGuestSync();
  139. if (isset($subscriberResult['message']) && isset($result['message'])) {
  140. $result['message'] = $result['message'] . ' - '
  141. . $subscriberResult['message'];
  142. }
  143. return $result;
  144. }
  145. /**
  146. * CRON FOR SUBSCRIBERS AND GUEST CONTACTS.
  147. *
  148. * @return array
  149. */
  150. public function subscribersAndGuestSync()
  151. {
  152. //sync subscribers
  153. $subscriberModel = $this->subscriberFactory->create();
  154. $result = $subscriberModel->sync();
  155. //un-subscribe suppressed contacts
  156. $subscriberModel->unsubscribe();
  157. //sync guests
  158. $this->guestFactory->create()->sync();
  159. return $result;
  160. }
  161. /**
  162. * CRON FOR CATALOG SYNC.
  163. *
  164. * @return array
  165. */
  166. public function catalogSync()
  167. {
  168. if ($this->jobHasAlreadyBeenRun('ddg_automation_catalog_sync')) {
  169. $message = 'Skipping ddg_automation_catalog_sync job run';
  170. $this->helper->log($message);
  171. return ['message' => $message];
  172. }
  173. $result = $this->catalogFactory->create()
  174. ->sync();
  175. return $result;
  176. }
  177. /**
  178. * CRON FOR EMAIL IMPORTER PROCESSOR.
  179. *
  180. * @return null
  181. */
  182. public function emailImporter()
  183. {
  184. if ($this->jobHasAlreadyBeenRun('ddg_automation_importer')) {
  185. $this->helper->log('Skipping ddg_automation_importer job run');
  186. return;
  187. }
  188. $this->importerFactory->create()->processQueue();
  189. }
  190. /**
  191. * CRON FOR SYNC REVIEWS and REGISTER ORDER REVIEW CAMPAIGNS.
  192. *
  193. * @return null
  194. */
  195. public function reviewsAndWishlist()
  196. {
  197. if ($this->jobHasAlreadyBeenRun('ddg_automation_reviews_and_wishlist')) {
  198. $this->helper->log('Skipping ddg_automation_reviews_and_wishlist job run');
  199. return;
  200. }
  201. //sync reviews
  202. $this->reviewSync();
  203. //sync wishlist
  204. $this->cronHelper->wishlistSync();
  205. }
  206. /**
  207. * Review sync.
  208. *
  209. * @return array
  210. */
  211. public function reviewSync()
  212. {
  213. return $this->cronHelper->reviewSync();
  214. }
  215. /**
  216. * CRON FOR ABANDONED CARTS.
  217. *
  218. * @return null
  219. */
  220. public function abandonedCarts()
  221. {
  222. if ($this->jobHasAlreadyBeenRun('ddg_automation_abandonedcarts')) {
  223. $this->helper->log('Skipping ddg_automation_abandonedcarts job run');
  224. return;
  225. }
  226. $this->quoteFactory->create()->processAbandonedCarts();
  227. }
  228. /**
  229. * CRON FOR AUTOMATION.
  230. *
  231. * @return null
  232. */
  233. public function syncAutomation()
  234. {
  235. if ($this->jobHasAlreadyBeenRun('ddg_automation_status')) {
  236. $this->helper->log('Skipping ddg_automation_status job run');
  237. return;
  238. }
  239. $this->automationFactory->create()->sync();
  240. }
  241. /**
  242. * Send email campaigns.
  243. *
  244. * @throws \Magento\Framework\Exception\LocalizedException
  245. *
  246. * @return null
  247. */
  248. public function sendCampaigns()
  249. {
  250. if ($this->jobHasAlreadyBeenRun('ddg_automation_campaign')) {
  251. $this->helper->log('Skipping ddg_automation_campaign job run');
  252. return;
  253. }
  254. $this->campaignFactory->create()->sendCampaigns();
  255. }
  256. /**
  257. * CRON FOR ORDER TRANSACTIONAL DATA.
  258. *
  259. * @return array
  260. */
  261. public function orderSync()
  262. {
  263. if ($this->jobHasAlreadyBeenRun('ddg_automation_order_sync')) {
  264. $message = 'Skipping ddg_automation_order_sync job run';
  265. $this->helper->log($message);
  266. return ['message' => $message];
  267. }
  268. // send order
  269. $orderResult = $this->syncOrderFactory->create()
  270. ->sync();
  271. return $orderResult;
  272. }
  273. /**
  274. * Cleaning for csv files and connector tables.
  275. *
  276. * @return string
  277. */
  278. public function cleaning()
  279. {
  280. if ($this->jobHasAlreadyBeenRun('ddg_automation_cleaner')) {
  281. $message = 'Skipping ddg_automation_cleaner job run';
  282. $this->helper->log($message);
  283. return $message;
  284. }
  285. //Clean tables
  286. $tables = [
  287. 'automation' => Schema::EMAIL_AUTOMATION_TABLE,
  288. 'importer' => Schema::EMAIL_IMPORTER_TABLE,
  289. 'campaign' => Schema::EMAIL_CAMPAIGN_TABLE,
  290. ];
  291. $message = 'Cleaning cron job result :';
  292. foreach ($tables as $key => $table) {
  293. $result = $this->importerResource->cleanup($table);
  294. $message .= " $result records removed from $key .";
  295. }
  296. $archivedFolder = $this->fileHelper->getArchiveFolder();
  297. $result = $this->fileHelper->deleteDir($archivedFolder);
  298. $message .= ' Deleting archived folder result : ' . $result;
  299. $this->helper->log($message);
  300. return $message;
  301. }
  302. /**
  303. * Check if already ran for same time
  304. *
  305. * @param string $jobCode
  306. * @return bool
  307. */
  308. private function jobHasAlreadyBeenRun($jobCode)
  309. {
  310. $currentRunningJob = $this->cronCollection->create()
  311. ->addFieldToFilter('job_code', $jobCode)
  312. ->addFieldToFilter('status', 'running')
  313. ->setPageSize(1);
  314. if ($currentRunningJob->getSize()) {
  315. $jobOfSameTypeAndScheduledAtDateAlreadyExecuted = $this->cronCollection->create()
  316. ->addFieldToFilter('job_code', $jobCode)
  317. ->addFieldToFilter('scheduled_at', $currentRunningJob->getFirstItem()->getScheduledAt())
  318. ->addFieldToFilter('status', ['in' => ['success', 'failed']]);
  319. return ($jobOfSameTypeAndScheduledAtDateAlreadyExecuted->getSize()) ? true : false;
  320. }
  321. return false;
  322. }
  323. /**
  324. * Sync the email templates from dotmailer.
  325. */
  326. public function syncEmailTemplates()
  327. {
  328. if ($this->jobHasAlreadyBeenRun('ddg_automation_email_templates')) {
  329. $message = 'Skipping ddg_automation_email_templates job run';
  330. $this->helper->log($message);
  331. return $message;
  332. }
  333. return $this->templateFactory->create()
  334. ->sync();
  335. }
  336. }