AbandonedCartSubject.php
2 years ago
CustomerSubject.php
5 months ago
OrderStatusChangeSubject.php
2 years ago
OrderSubject.php
2 years ago
index.php
3 years ago
CustomerSubject.php
86 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Automation\Integrations\WooCommerce\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\WooCommerce\Fields\CustomerFieldsFactory; |
| 13 | use MailPoet\Automation\Integrations\WooCommerce\Payloads\CustomerPayload; |
| 14 | use MailPoet\NotFoundException; |
| 15 | use MailPoet\Validator\Builder; |
| 16 | use MailPoet\Validator\Schema\ObjectSchema; |
| 17 | use MailPoet\WPCOM\DotcomHelperFunctions; |
| 18 | use WC_Customer; |
| 19 | use WC_Order; |
| 20 | |
| 21 | /** |
| 22 | * @implements Subject<CustomerPayload> |
| 23 | */ |
| 24 | class CustomerSubject implements Subject { |
| 25 | const KEY = 'woocommerce:customer'; |
| 26 | |
| 27 | /** @var CustomerFieldsFactory */ |
| 28 | private $customerFieldsFactory; |
| 29 | |
| 30 | /** @var DotcomHelperFunctions */ |
| 31 | private $dotcomHelperFunctions; |
| 32 | |
| 33 | public function __construct( |
| 34 | CustomerFieldsFactory $customerFieldsFactory, |
| 35 | DotcomHelperFunctions $dotcomHelperFunctions |
| 36 | ) { |
| 37 | $this->customerFieldsFactory = $customerFieldsFactory; |
| 38 | $this->dotcomHelperFunctions = $dotcomHelperFunctions; |
| 39 | } |
| 40 | |
| 41 | public function getName(): string { |
| 42 | if ($this->dotcomHelperFunctions->isGarden()) { |
| 43 | // translators: automation subject (entity entering automation) title |
| 44 | return __('Customer', 'mailpoet'); |
| 45 | } |
| 46 | // translators: automation subject (entity entering automation) title |
| 47 | return __('WooCommerce customer', 'mailpoet'); |
| 48 | } |
| 49 | |
| 50 | public function getKey(): string { |
| 51 | return self::KEY; |
| 52 | } |
| 53 | |
| 54 | public function getArgsSchema(): ObjectSchema { |
| 55 | return Builder::object([ |
| 56 | 'customer_id' => Builder::integer()->required(), |
| 57 | ]); |
| 58 | } |
| 59 | |
| 60 | public function getPayload(SubjectData $subjectData): Payload { |
| 61 | $args = $subjectData->getArgs(); |
| 62 | $customerId = isset($args['customer_id']) ? (int)$args['customer_id'] : null; |
| 63 | $orderId = isset($args['order_id']) ? (int)$args['order_id'] : null; |
| 64 | |
| 65 | $order = $orderId === null ? null : wc_get_order($orderId); |
| 66 | $order = $order instanceof WC_Order ? $order : null; |
| 67 | |
| 68 | if (!$customerId) { |
| 69 | return new CustomerPayload(null, $order); |
| 70 | } |
| 71 | |
| 72 | $customer = new WC_Customer($customerId); |
| 73 | if (!$customer->get_id()) { |
| 74 | // translators: %d is the ID of the customer. |
| 75 | throw NotFoundException::create()->withMessage(sprintf(__("Customer with ID '%d' not found.", 'mailpoet'), $customerId)); |
| 76 | } |
| 77 | |
| 78 | return new CustomerPayload($customer, $order); |
| 79 | } |
| 80 | |
| 81 | /** @return Field[] */ |
| 82 | public function getFields(): array { |
| 83 | return $this->customerFieldsFactory->getFields(); |
| 84 | } |
| 85 | } |
| 86 |