NewsletterLinkSubject.php
2 years ago
SegmentSubject.php
5 months ago
SubscriberSubject.php
5 months ago
index.php
3 years ago
SubscriberSubject.php
79 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\Subjects; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Field; |
| 9 | use MailPoet\Automation\Engine\Data\Subject as SubjectData; |
| 10 | use MailPoet\Automation\Engine\Integration\Payload; |
| 11 | use MailPoet\Automation\Engine\Integration\Subject; |
| 12 | use MailPoet\Automation\Integrations\MailPoet\Fields\SubscriberFieldsFactory; |
| 13 | use MailPoet\Automation\Integrations\MailPoet\Payloads\SubscriberPayload; |
| 14 | use MailPoet\NotFoundException; |
| 15 | use MailPoet\Subscribers\SubscribersRepository; |
| 16 | use MailPoet\Validator\Builder; |
| 17 | use MailPoet\Validator\Schema\ObjectSchema; |
| 18 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 19 | |
| 20 | /** |
| 21 | * @implements Subject<SubscriberPayload> |
| 22 | */ |
| 23 | class SubscriberSubject implements Subject { |
| 24 | const KEY = 'mailpoet:subscriber'; |
| 25 | |
| 26 | /** @var SubscriberFieldsFactory */ |
| 27 | private $subscriberFieldsFactory; |
| 28 | |
| 29 | /** @var SubscribersRepository */ |
| 30 | private $subscribersRepository; |
| 31 | |
| 32 | /** @var DotcomHelperFunctions */ |
| 33 | private $dotcomHelperFunctions; |
| 34 | |
| 35 | public function __construct( |
| 36 | SubscriberFieldsFactory $subscriberFieldsFactory, |
| 37 | SubscribersRepository $subscribersRepository, |
| 38 | DotcomHelperFunctions $dotcomHelperFunctions |
| 39 | ) { |
| 40 | $this->subscriberFieldsFactory = $subscriberFieldsFactory; |
| 41 | $this->subscribersRepository = $subscribersRepository; |
| 42 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 43 | } |
| 44 | |
| 45 | public function getKey(): string { |
| 46 | return self::KEY; |
| 47 | } |
| 48 | |
| 49 | public function getName(): string { |
| 50 | if ($this->dotcomHelperFunctions->isGarden()) { |
| 51 | // translators: automation subject (entity entering automation) title |
| 52 | return __('Subscriber', 'mailpoet'); |
| 53 | } |
| 54 | // translators: automation subject (entity entering automation) title |
| 55 | return __('MailPoet subscriber', 'mailpoet'); |
| 56 | } |
| 57 | |
| 58 | public function getArgsSchema(): ObjectSchema { |
| 59 | return Builder::object([ |
| 60 | 'subscriber_id' => Builder::integer()->required(), |
| 61 | ]); |
| 62 | } |
| 63 | |
| 64 | public function getPayload(SubjectData $subjectData): Payload { |
| 65 | $id = $subjectData->getArgs()['subscriber_id']; |
| 66 | $subscriber = $this->subscribersRepository->findOneById($id); |
| 67 | if (!$subscriber) { |
| 68 | // translators: %d is the ID. |
| 69 | throw NotFoundException::create()->withMessage(sprintf(__("Subscriber with ID '%d' not found.", 'mailpoet'), $id)); |
| 70 | } |
| 71 | return new SubscriberPayload($subscriber); |
| 72 | } |
| 73 | |
| 74 | /** @return Field[] */ |
| 75 | public function getFields(): array { |
| 76 | return $this->subscriberFieldsFactory->getFields(); |
| 77 | } |
| 78 | } |
| 79 |