mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
SubjectTransformers
/
CommentSubjectToSubscriberSubjectTransformer.php
CommentSubjectToSubscriberSubjectTransformer.php
2 years ago
CustomerSubjectToSubscriberSubjectTransformer.php
2 months ago
OrderSubjectToSegmentSubjectTransformer.php
3 years ago
OrderSubjectToSubscriberSubjectTransformer.php
2 months ago
SubscriberSubjectToWordPressUserSubjectTransformer.php
2 months ago
index.php
3 years ago
CommentSubjectToSubscriberSubjectTransformer.php
68 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\MailPoet\SubjectTransformers; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Automation\Engine\Data\Subject; |
| 9 | use MailPoet\Automation\Engine\Integration\SubjectTransformer; |
| 10 | use MailPoet\Automation\Engine\WordPress; |
| 11 | use MailPoet\Automation\Integrations\MailPoet\Subjects\SubscriberSubject; |
| 12 | use MailPoet\Automation\Integrations\WordPress\Subjects\CommentSubject; |
| 13 | use MailPoet\Subscribers\SubscribersRepository; |
| 14 | |
| 15 | class CommentSubjectToSubscriberSubjectTransformer implements SubjectTransformer { |
| 16 | |
| 17 | /** @var WordPress */ |
| 18 | private $wp; |
| 19 | |
| 20 | /** @var SubscribersRepository */ |
| 21 | private $subscribersRepository; |
| 22 | |
| 23 | public function __construct( |
| 24 | WordPress $wp, |
| 25 | SubscribersRepository $subscribersRepository |
| 26 | ) { |
| 27 | $this->wp = $wp; |
| 28 | $this->subscribersRepository = $subscribersRepository; |
| 29 | } |
| 30 | |
| 31 | public function transform(Subject $data): ?Subject { |
| 32 | |
| 33 | if ($this->accepts() !== $data->getKey()) { |
| 34 | throw new \InvalidArgumentException('Invalid subject type'); |
| 35 | } |
| 36 | $commentId = (int)$data->getArgs()['comment_id']; |
| 37 | $comment = $this->wp->getComment($commentId); |
| 38 | if (!$comment instanceof \WP_Comment) { |
| 39 | return null; |
| 40 | } |
| 41 | //phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps |
| 42 | $email = $comment->comment_author_email; |
| 43 | if (!$this->wp->isEmail($email)) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | $subscriber = $this->subscribersRepository->findOneBy(['email' => $email]); |
| 48 | if (!$subscriber) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return new Subject( |
| 53 | SubscriberSubject::KEY, |
| 54 | [ |
| 55 | 'subscriber_id' => $subscriber->getId(), |
| 56 | ] |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | public function returns(): string { |
| 61 | return SubscriberSubject::KEY; |
| 62 | } |
| 63 | |
| 64 | public function accepts(): string { |
| 65 | return CommentSubject::KEY; |
| 66 | } |
| 67 | } |
| 68 |