mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
Fields
/
SubscriberAutomationFieldsFactory.php
NewsletterLinkFieldsFactory.php
2 years ago
SubscriberAutomationFieldsFactory.php
1 year ago
SubscriberCustomFieldsFactory.php
2 months ago
SubscriberFieldsFactory.php
2 months ago
SubscriberStatisticFieldsFactory.php
2 years ago
index.php
3 years ago
SubscriberAutomationFieldsFactory.php
78 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Fields; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Automation; |
| 9 | use MailPoet\Automation\Engine\Data\AutomationRun; |
| 10 | use MailPoet\Automation\Engine\Data\Field; |
| 11 | use MailPoet\Automation\Engine\Data\Subject; |
| 12 | use MailPoet\Automation\Engine\Storage\AutomationStorage; |
| 13 | use MailPoet\Automation\Integrations\MailPoet\Payloads\SubscriberPayload; |
| 14 | use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject; |
| 15 | |
| 16 | class SubscriberAutomationFieldsFactory { |
| 17 | /** @var AutomationStorage */ |
| 18 | private $automationStorage; |
| 19 | |
| 20 | public function __construct( |
| 21 | AutomationStorage $automationStorage |
| 22 | ) { |
| 23 | $this->automationStorage = $automationStorage; |
| 24 | } |
| 25 | |
| 26 | /** @return Field[] */ |
| 27 | public function getFields(): array { |
| 28 | $automations = $this->automationStorage->getAutomations( |
| 29 | array_diff(Automation::STATUS_ALL, [Automation::STATUS_TRASH]) |
| 30 | ); |
| 31 | $args = [ |
| 32 | 'options' => array_map(function (Automation $automation) { |
| 33 | return [ |
| 34 | 'id' => $automation->getId(), |
| 35 | 'name' => $automation->getName() . " (#{$automation->getId()})", |
| 36 | ]; |
| 37 | }, $automations), |
| 38 | 'params' => ['in_the_last'], |
| 39 | ]; |
| 40 | |
| 41 | return [ |
| 42 | new Field( |
| 43 | 'mailpoet:subscriber:automations-entered', |
| 44 | Field::TYPE_ENUM_ARRAY, |
| 45 | __('Automations — entered', 'mailpoet'), |
| 46 | function (SubscriberPayload $payload, array $params = []) { |
| 47 | return $this->getAutomationIds($payload, null, $params); |
| 48 | }, |
| 49 | $args |
| 50 | ), |
| 51 | new Field( |
| 52 | 'mailpoet:subscriber:automations-processing', |
| 53 | Field::TYPE_ENUM_ARRAY, |
| 54 | __('Automations — processing', 'mailpoet'), |
| 55 | function (SubscriberPayload $payload, array $params = []) { |
| 56 | return $this->getAutomationIds($payload, [AutomationRun::STATUS_RUNNING], $params); |
| 57 | }, |
| 58 | $args |
| 59 | ), |
| 60 | new Field( |
| 61 | 'mailpoet:subscriber:automations-exited', |
| 62 | Field::TYPE_ENUM_ARRAY, |
| 63 | __('Automations — exited', 'mailpoet'), |
| 64 | function (SubscriberPayload $payload, array $params = []) { |
| 65 | return $this->getAutomationIds($payload, [AutomationRun::STATUS_COMPLETE], $params); |
| 66 | }, |
| 67 | $args |
| 68 | ), |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | private function getAutomationIds(SubscriberPayload $payload, ?array $status = null, array $params = []): array { |
| 73 | $inTheLastSeconds = isset($params['in_the_last']) ? (int)$params['in_the_last'] : null; |
| 74 | $subject = new Subject(SubscriberSubject::KEY, ['subscriber_id' => $payload->getId()]); |
| 75 | return $this->automationStorage->getAutomationIdsBySubject($subject, $status, $inTheLastSeconds); |
| 76 | } |
| 77 | } |
| 78 |