123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace Dotdigitalgroup\Email\Model;
- use Dotdigitalgroup\Email\Setup\Schema;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Cron
- {
- /**
- * @var Email\TemplateFactory
- */
- private $templateFactory;
- /**
- * @var Apiconnector\ContactFactory
- */
- private $contactFactory;
- /**
- * @var Sync\AutomationFactory
- */
- private $automationFactory;
- /**
- * @var ImporterFactory
- */
- private $importerFactory;
- /**
- * @var Sync\CatalogFactory
- */
- private $catalogFactory;
- /**
- * @var Newsletter\SubscriberFactory
- */
- private $subscriberFactory;
- /**
- * @var Customer\GuestFactory
- */
- private $guestFactory;
- /**
- * @var Sales\QuoteFactory
- */
- private $quoteFactory;
- /**
- * @var Sync\OrderFactory
- */
- private $syncOrderFactory;
- /**
- * @var Sync\CampaignFactory
- */
- private $campaignFactory;
- /**
- * @var \Dotdigitalgroup\Email\Helper\Data
- */
- private $helper;
- /**
- * @var \Dotdigitalgroup\Email\Helper\File
- */
- private $fileHelper;
- /**
- * @var ResourceModel\Importer
- */
- private $importerResource;
- /**
- * @var ResourceModel\Cron\CollectionFactory
- */
- private $cronCollection;
- /**
- * @var Cron\CronSub
- */
- private $cronHelper;
- /**
- * Cron constructor.
- *
- * @param Sync\CampaignFactory $campaignFactory
- * @param Sync\OrderFactory $syncOrderFactory
- * @param Sales\QuoteFactory $quoteFactory
- * @param Customer\GuestFactory $guestFactory
- * @param Newsletter\SubscriberFactory $subscriberFactory
- * @param Sync\CatalogFactory $catalogFactorty
- * @param ImporterFactory $importerFactory
- * @param Sync\AutomationFactory $automationFactory
- * @param Apiconnector\ContactFactory $contact
- * @param \Dotdigitalgroup\Email\Helper\Data $helper
- * @param \Dotdigitalgroup\Email\Helper\File $fileHelper
- * @param ResourceModel\Importer $importerResource
- * @param ResourceModel\Cron\CollectionFactory $cronCollection
- * @param Cron\CronSubFactory $cronSubFactory
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Dotdigitalgroup\Email\Model\Sync\CampaignFactory $campaignFactory,
- \Dotdigitalgroup\Email\Model\Sync\OrderFactory $syncOrderFactory,
- \Dotdigitalgroup\Email\Model\Sales\QuoteFactory $quoteFactory,
- \Dotdigitalgroup\Email\Model\Customer\GuestFactory $guestFactory,
- \Dotdigitalgroup\Email\Model\Newsletter\SubscriberFactory $subscriberFactory,
- \Dotdigitalgroup\Email\Model\Sync\CatalogFactory $catalogFactorty,
- \Dotdigitalgroup\Email\Model\ImporterFactory $importerFactory,
- \Dotdigitalgroup\Email\Model\Sync\AutomationFactory $automationFactory,
- \Dotdigitalgroup\Email\Model\Apiconnector\ContactFactory $contact,
- \Dotdigitalgroup\Email\Helper\Data $helper,
- \Dotdigitalgroup\Email\Helper\File $fileHelper,
- \Dotdigitalgroup\Email\Model\ResourceModel\Importer $importerResource,
- \Dotdigitalgroup\Email\Model\Email\TemplateFactory $templateFactory,
- \Dotdigitalgroup\Email\Model\ResourceModel\Cron\CollectionFactory $cronCollection,
- Cron\CronSubFactory $cronSubFactory
- ) {
- $this->campaignFactory = $campaignFactory;
- $this->syncOrderFactory = $syncOrderFactory;
- $this->quoteFactory = $quoteFactory;
- $this->guestFactory = $guestFactory;
- $this->subscriberFactory = $subscriberFactory;
- $this->catalogFactory = $catalogFactorty;
- $this->importerFactory = $importerFactory;
- $this->automationFactory = $automationFactory;
- $this->contactFactory = $contact;
- $this->helper = $helper;
- $this->fileHelper = $fileHelper;
- $this->importerResource = $importerResource;
- $this->cronCollection = $cronCollection;
- $this->templateFactory = $templateFactory;
- $this->cronHelper = $cronSubFactory->create();
- }
- /**
- * CRON FOR CONTACTS SYNC.
- *
- * @return array
- */
- public function contactSync()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_customer_subscriber_guest_sync')) {
- $message = 'Skipping ddg_automation_customer_subscriber_guest_sync job run';
- $this->helper->log($message);
- return ['message' => $message];
- }
- //run the sync for contacts
- $result = $this->contactFactory->create()
- ->sync();
- //run subscribers and guests sync
- $subscriberResult = $this->subscribersAndGuestSync();
- if (isset($subscriberResult['message']) && isset($result['message'])) {
- $result['message'] = $result['message'] . ' - '
- . $subscriberResult['message'];
- }
- return $result;
- }
- /**
- * CRON FOR SUBSCRIBERS AND GUEST CONTACTS.
- *
- * @return array
- */
- public function subscribersAndGuestSync()
- {
- //sync subscribers
- $subscriberModel = $this->subscriberFactory->create();
- $result = $subscriberModel->sync();
- //un-subscribe suppressed contacts
- $subscriberModel->unsubscribe();
- //sync guests
- $this->guestFactory->create()->sync();
- return $result;
- }
- /**
- * CRON FOR CATALOG SYNC.
- *
- * @return array
- */
- public function catalogSync()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_catalog_sync')) {
- $message = 'Skipping ddg_automation_catalog_sync job run';
- $this->helper->log($message);
- return ['message' => $message];
- }
- $result = $this->catalogFactory->create()
- ->sync();
- return $result;
- }
- /**
- * CRON FOR EMAIL IMPORTER PROCESSOR.
- *
- * @return null
- */
- public function emailImporter()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_importer')) {
- $this->helper->log('Skipping ddg_automation_importer job run');
- return;
- }
- $this->importerFactory->create()->processQueue();
- }
- /**
- * CRON FOR SYNC REVIEWS and REGISTER ORDER REVIEW CAMPAIGNS.
- *
- * @return null
- */
- public function reviewsAndWishlist()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_reviews_and_wishlist')) {
- $this->helper->log('Skipping ddg_automation_reviews_and_wishlist job run');
- return;
- }
- //sync reviews
- $this->reviewSync();
- //sync wishlist
- $this->cronHelper->wishlistSync();
- }
- /**
- * Review sync.
- *
- * @return array
- */
- public function reviewSync()
- {
- return $this->cronHelper->reviewSync();
- }
- /**
- * CRON FOR ABANDONED CARTS.
- *
- * @return null
- */
- public function abandonedCarts()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_abandonedcarts')) {
- $this->helper->log('Skipping ddg_automation_abandonedcarts job run');
- return;
- }
- $this->quoteFactory->create()->processAbandonedCarts();
- }
- /**
- * CRON FOR AUTOMATION.
- *
- * @return null
- */
- public function syncAutomation()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_status')) {
- $this->helper->log('Skipping ddg_automation_status job run');
- return;
- }
- $this->automationFactory->create()->sync();
- }
- /**
- * Send email campaigns.
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- *
- * @return null
- */
- public function sendCampaigns()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_campaign')) {
- $this->helper->log('Skipping ddg_automation_campaign job run');
- return;
- }
- $this->campaignFactory->create()->sendCampaigns();
- }
- /**
- * CRON FOR ORDER TRANSACTIONAL DATA.
- *
- * @return array
- */
- public function orderSync()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_order_sync')) {
- $message = 'Skipping ddg_automation_order_sync job run';
- $this->helper->log($message);
- return ['message' => $message];
- }
- // send order
- $orderResult = $this->syncOrderFactory->create()
- ->sync();
- return $orderResult;
- }
- /**
- * Cleaning for csv files and connector tables.
- *
- * @return string
- */
- public function cleaning()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_cleaner')) {
- $message = 'Skipping ddg_automation_cleaner job run';
- $this->helper->log($message);
- return $message;
- }
- //Clean tables
- $tables = [
- 'automation' => Schema::EMAIL_AUTOMATION_TABLE,
- 'importer' => Schema::EMAIL_IMPORTER_TABLE,
- 'campaign' => Schema::EMAIL_CAMPAIGN_TABLE,
- ];
- $message = 'Cleaning cron job result :';
- foreach ($tables as $key => $table) {
- $result = $this->importerResource->cleanup($table);
- $message .= " $result records removed from $key .";
- }
- $archivedFolder = $this->fileHelper->getArchiveFolder();
- $result = $this->fileHelper->deleteDir($archivedFolder);
- $message .= ' Deleting archived folder result : ' . $result;
- $this->helper->log($message);
- return $message;
- }
- /**
- * Check if already ran for same time
- *
- * @param string $jobCode
- * @return bool
- */
- private function jobHasAlreadyBeenRun($jobCode)
- {
- $currentRunningJob = $this->cronCollection->create()
- ->addFieldToFilter('job_code', $jobCode)
- ->addFieldToFilter('status', 'running')
- ->setPageSize(1);
- if ($currentRunningJob->getSize()) {
- $jobOfSameTypeAndScheduledAtDateAlreadyExecuted = $this->cronCollection->create()
- ->addFieldToFilter('job_code', $jobCode)
- ->addFieldToFilter('scheduled_at', $currentRunningJob->getFirstItem()->getScheduledAt())
- ->addFieldToFilter('status', ['in' => ['success', 'failed']]);
- return ($jobOfSameTypeAndScheduledAtDateAlreadyExecuted->getSize()) ? true : false;
- }
- return false;
- }
- /**
- * Sync the email templates from dotmailer.
- */
- public function syncEmailTemplates()
- {
- if ($this->jobHasAlreadyBeenRun('ddg_automation_email_templates')) {
- $message = 'Skipping ddg_automation_email_templates job run';
- $this->helper->log($message);
- return $message;
- }
- return $this->templateFactory->create()
- ->sync();
- }
- }
|