Events
2 months ago
WooCommerce.php
2 years ago
WooCommerceEventFactory.php
3 years ago
index.php
3 years ago
WooCommerceEventFactory.php
43 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\AutomaticEmails\WooCommerce; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\AutomaticEmails\WooCommerce\Events\AbandonedCart; |
| 9 | use MailPoet\AutomaticEmails\WooCommerce\Events\FirstPurchase; |
| 10 | use MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedInCategory; |
| 11 | use MailPoet\AutomaticEmails\WooCommerce\Events\PurchasedProduct; |
| 12 | use MailPoet\DI\ContainerWrapper; |
| 13 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 14 | |
| 15 | class WooCommerceEventFactory { |
| 16 | public const EVENTS_MAP = [ |
| 17 | 'AbandonedCart' => AbandonedCart::class, |
| 18 | 'FirstPurchase' => FirstPurchase::class, |
| 19 | 'PurchasedInCategory' => PurchasedInCategory::class, |
| 20 | 'PurchasedProduct' => PurchasedProduct::class, |
| 21 | ]; |
| 22 | |
| 23 | /** @var ContainerWrapper */ |
| 24 | private $container; |
| 25 | |
| 26 | public function __construct( |
| 27 | ContainerWrapper $container |
| 28 | ) { |
| 29 | $this->container = $container; |
| 30 | } |
| 31 | |
| 32 | /** @return object|null */ |
| 33 | public function createEvent(string $eventName) { |
| 34 | $eventClass = self::EVENTS_MAP[$eventName] ?? null; |
| 35 | |
| 36 | try { |
| 37 | return $eventClass ? $this->container->get($eventClass) : null; |
| 38 | } catch (ServiceNotFoundException $e) { |
| 39 | return null; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 |