KeyCheckWorker.php
1 year ago
PremiumKeyCheck.php
3 years ago
SendingServiceKeyCheck.php
1 year ago
index.php
3 years ago
SendingServiceKeyCheck.php
71 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Cron\Workers\KeyCheck; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\ServicesChecker; |
| 9 | use MailPoet\Cron\CronWorkerScheduler; |
| 10 | use MailPoet\InvalidStateException; |
| 11 | use MailPoet\Mailer\Mailer; |
| 12 | use MailPoet\Mailer\MailerLog; |
| 13 | use MailPoet\Services\Bridge; |
| 14 | use MailPoet\Settings\SettingsController; |
| 15 | use MailPoetVendor\Carbon\Carbon; |
| 16 | |
| 17 | class SendingServiceKeyCheck extends KeyCheckWorker { |
| 18 | const TASK_TYPE = 'sending_service_key_check'; |
| 19 | |
| 20 | /** @var SettingsController */ |
| 21 | private $settings; |
| 22 | |
| 23 | /** @var ServicesChecker */ |
| 24 | private $servicesChecker; |
| 25 | |
| 26 | public function __construct( |
| 27 | SettingsController $settings, |
| 28 | ServicesChecker $servicesChecker, |
| 29 | CronWorkerScheduler $cronWorkerScheduler |
| 30 | ) { |
| 31 | $this->settings = $settings; |
| 32 | $this->servicesChecker = $servicesChecker; |
| 33 | parent::__construct($cronWorkerScheduler); |
| 34 | } |
| 35 | |
| 36 | public function checkProcessingRequirements() { |
| 37 | return Bridge::isMPSendingServiceEnabled(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @return \DateTimeInterface|Carbon |
| 42 | */ |
| 43 | public function getNextRunDate() { |
| 44 | // when key pending approval, check key sate every hour |
| 45 | if ($this->servicesChecker->isMailPoetAPIKeyPendingApproval()) { |
| 46 | $date = Carbon::now()->millisecond(0); |
| 47 | return $date->addHour(); |
| 48 | } |
| 49 | return parent::getNextRunDate(); |
| 50 | } |
| 51 | |
| 52 | public function checkKey() { |
| 53 | // for phpstan because we set bridge property in the init function |
| 54 | if (!$this->bridge) { |
| 55 | throw new InvalidStateException('The class was not initialized properly. Please call the Init method before.'); |
| 56 | }; |
| 57 | |
| 58 | $wasPendingApproval = $this->servicesChecker->isMailPoetAPIKeyPendingApproval(); |
| 59 | |
| 60 | $mssKey = $this->settings->get(Mailer::MAILER_CONFIG_SETTING_NAME)['mailpoet_api_key']; |
| 61 | $result = $this->bridge->checkMSSKey($mssKey); |
| 62 | $this->bridge->storeMSSKeyAndState($mssKey, $result); |
| 63 | |
| 64 | $isPendingApproval = $this->servicesChecker->isMailPoetAPIKeyPendingApproval(); |
| 65 | if ($wasPendingApproval && !$isPendingApproval) { |
| 66 | MailerLog::resumeSending(); |
| 67 | } |
| 68 | return $result; |
| 69 | } |
| 70 | } |
| 71 |