mailpoet
/
lib
/
Automation
/
Integrations
/
MailPoet
/
SubjectTransformers
/
OrderSubjectToSubscriberSubjectTransformer.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
OrderSubjectToSubscriberSubjectTransformer.php
96 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\Integrations\MailPoet\Subjects\SubscriberSubject; |
| 11 | use MailPoet\Automation\Integrations\WooCommerce\Subjects\OrderSubject; |
| 12 | use MailPoet\Automation\Integrations\WooCommerce\WooCommerce; |
| 13 | use MailPoet\Entities\SubscriberEntity; |
| 14 | use MailPoet\Segments; |
| 15 | use MailPoet\Subscribers\SubscribersRepository; |
| 16 | |
| 17 | class OrderSubjectToSubscriberSubjectTransformer implements SubjectTransformer { |
| 18 | |
| 19 | /** @var SubscribersRepository */ |
| 20 | private $subscribersRepository; |
| 21 | |
| 22 | /** @var Segments\WooCommerce */ |
| 23 | private $woocommerce; |
| 24 | |
| 25 | /** @var WooCommerce */ |
| 26 | private $woocommerceHelper; |
| 27 | |
| 28 | public function __construct( |
| 29 | SubscribersRepository $subscribersRepository, |
| 30 | Segments\WooCommerce $woocommerce, |
| 31 | WooCommerce $woocommerceHelper |
| 32 | ) { |
| 33 | $this->subscribersRepository = $subscribersRepository; |
| 34 | $this->woocommerce = $woocommerce; |
| 35 | $this->woocommerceHelper = $woocommerceHelper; |
| 36 | } |
| 37 | |
| 38 | public function transform(Subject $data): ?Subject { |
| 39 | if ($this->accepts() !== $data->getKey()) { |
| 40 | throw new \InvalidArgumentException('Invalid subject type'); |
| 41 | } |
| 42 | |
| 43 | $subscriber = $this->findOrCreateSubscriber($data); |
| 44 | if (!$subscriber instanceof SubscriberEntity) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | return new Subject(SubscriberSubject::KEY, ['subscriber_id' => $subscriber->getId()]); |
| 49 | } |
| 50 | |
| 51 | public function accepts(): string { |
| 52 | return OrderSubject::KEY; |
| 53 | } |
| 54 | |
| 55 | public function returns(): string { |
| 56 | return SubscriberSubject::KEY; |
| 57 | } |
| 58 | |
| 59 | private function findOrCreateSubscriber(Subject $order): ?SubscriberEntity { |
| 60 | $subscriber = $this->findSubscriber($order); |
| 61 | if ($subscriber) { |
| 62 | return $subscriber; |
| 63 | } |
| 64 | |
| 65 | $orderId = $order->getArgs()['order_id'] ?? null; |
| 66 | if (!$orderId) { |
| 67 | return null; |
| 68 | } |
| 69 | $this->woocommerce->synchronizeGuestCustomer($orderId); |
| 70 | |
| 71 | return $this->findSubscriber($order); |
| 72 | } |
| 73 | |
| 74 | private function findSubscriber(Subject $order): ?SubscriberEntity { |
| 75 | $orderId = $order->getArgs()['order_id'] ?? null; |
| 76 | if (!$orderId) { |
| 77 | return null; |
| 78 | } |
| 79 | $wcOrder = $this->woocommerceHelper->wcGetOrder($orderId); |
| 80 | if (!$wcOrder instanceof \WC_Order) { |
| 81 | return null; |
| 82 | } |
| 83 | $billingEmail = $wcOrder->get_billing_email(); |
| 84 | if ($billingEmail) { |
| 85 | return $this->subscribersRepository->findOneBy(['email' => $billingEmail]); |
| 86 | } |
| 87 | |
| 88 | $userId = $wcOrder->get_user_id(); |
| 89 | if (!$userId) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | return $this->subscribersRepository->findOneBy(['wpUserId' => $userId]); |
| 94 | } |
| 95 | } |
| 96 |