Automations
1 year ago
KeyCheck
1 year ago
SendingQueue
5 days ago
StatsNotifications
2 months ago
AuthorizedSendingEmailsCheck.php
3 years ago
BackfillEngagementData.php
1 year ago
Bounce.php
5 days ago
BounceTaskSubscribersCleanup.php
1 month ago
BulkConfirmationEmailResend.php
2 months ago
ExportFilesCleanup.php
2 months ago
InactiveSubscribersMaintenance.php
2 weeks ago
LogCleanup.php
10 months ago
Mixpanel.php
9 months ago
NewsletterTemplateThumbnails.php
1 year ago
ReEngagementEmailsScheduler.php
1 year ago
Scheduler.php
2 months ago
SendingQueueBodyCleanup.php
2 months ago
SendingTaskSubscribersCleanup.php
2 months ago
SimpleWorker.php
1 year ago
StatisticsExport.php
2 months ago
SubscriberLimitNotificationWorker.php
2 months ago
SubscriberLinkTokens.php
1 year ago
SubscribersCountCacheRecalculation.php
2 weeks ago
SubscribersEngagementScore.php
4 days ago
SubscribersLastEngagement.php
2 months ago
SubscribersSegmentsCountSync.php
2 weeks ago
SubscribersStatsReport.php
1 year ago
Tracks.php
9 months ago
UnconfirmedSubscribersCleanup.php
2 months ago
UnsubscribeTokens.php
1 month ago
WooCommercePastOrders.php
1 year ago
WooCommerceSync.php
3 years ago
WorkersFactory.php
2 weeks ago
index.php
3 years ago
SubscribersStatsReport.php
64 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\ServicesChecker; |
| 9 | use MailPoet\Cron\CronWorkerScheduler; |
| 10 | use MailPoet\Entities\ScheduledTaskEntity; |
| 11 | use MailPoet\Services\SubscribersCountReporter; |
| 12 | use MailPoetVendor\Carbon\Carbon; |
| 13 | |
| 14 | class SubscribersStatsReport extends SimpleWorker { |
| 15 | const TASK_TYPE = 'subscribers_stats_report'; |
| 16 | |
| 17 | /** @var SubscribersCountReporter */ |
| 18 | private $subscribersCountReporter; |
| 19 | |
| 20 | /** @var ServicesChecker */ |
| 21 | private $serviceChecker; |
| 22 | |
| 23 | /** @var CronWorkerScheduler */ |
| 24 | private $workerScheduler; |
| 25 | |
| 26 | public function __construct( |
| 27 | SubscribersCountReporter $subscribersCountReporter, |
| 28 | ServicesChecker $servicesChecker, |
| 29 | CronWorkerScheduler $workerScheduler |
| 30 | ) { |
| 31 | parent::__construct(); |
| 32 | $this->subscribersCountReporter = $subscribersCountReporter; |
| 33 | $this->serviceChecker = $servicesChecker; |
| 34 | $this->workerScheduler = $workerScheduler; |
| 35 | } |
| 36 | |
| 37 | public function checkProcessingRequirements() { |
| 38 | return (bool)$this->serviceChecker->getValidAccountKey(); |
| 39 | } |
| 40 | |
| 41 | public function processTaskStrategy(ScheduledTaskEntity $task, $timer): bool { |
| 42 | $key = $this->serviceChecker->getValidAccountKey(); |
| 43 | if ($key === null) { |
| 44 | return false; |
| 45 | } |
| 46 | $result = $this->subscribersCountReporter->report($key); |
| 47 | // We have a valid key, but request failed |
| 48 | if ($result === false) { |
| 49 | $this->workerScheduler->rescheduleProgressively($task); |
| 50 | } |
| 51 | return $result; |
| 52 | } |
| 53 | |
| 54 | public function getNextRunDate() { |
| 55 | $date = Carbon::now()->millisecond(0); |
| 56 | // Spread the check within 6 hours after midnight so that all plugins don't ping the service at the same time |
| 57 | return $date->startOfDay() |
| 58 | ->addDay() |
| 59 | ->addHours(rand(0, 5)) |
| 60 | ->addMinutes(rand(0, 59)) |
| 61 | ->addSeconds(rand(0, 59)); |
| 62 | } |
| 63 | } |
| 64 |