From 0c5f8a37c03a4eeb011a2d2bb033d031e5f188cc Mon Sep 17 00:00:00 2001 From: Oleksii Skorobogatko Date: Sat, 31 Aug 2024 21:18:36 +0300 Subject: [PATCH] #4 subscriber notifications, wip --- config/services.yaml | 4 ++ .../NotifySubscribersListener.php | 23 +++++++++++ ...ificationHandlerNotRegisteredException.php | 16 ++++++++ .../Notification/HttpNotificationHandler.php | 39 ++++++++++++++++++ .../NotificationHandlerCollection.php | 41 +++++++++++++++++++ .../NotificationHandlerInterface.php | 13 ++++++ 6 files changed, 136 insertions(+) create mode 100644 src/EventListener/NotifySubscribersListener.php create mode 100644 src/Subscriber/Exception/NotificationHandlerNotRegisteredException.php create mode 100644 src/Subscriber/Notification/HttpNotificationHandler.php create mode 100644 src/Subscriber/Notification/NotificationHandlerCollection.php create mode 100644 src/Subscriber/Notification/NotificationHandlerInterface.php diff --git a/config/services.yaml b/config/services.yaml index aff476e..e8e28ec 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -36,3 +36,7 @@ services: App\Order\OrderTotalAmountCalculator: arguments: $currencyCode: 'UAH' + + App\Subscriber\Notification\HttpNotificationHandler: + tags: + - { name: 'app.subscriber.notification', type: 'http' } diff --git a/src/EventListener/NotifySubscribersListener.php b/src/EventListener/NotifySubscribersListener.php new file mode 100644 index 0000000..2b1c843 --- /dev/null +++ b/src/EventListener/NotifySubscribersListener.php @@ -0,0 +1,23 @@ +subscriberRepository->getList($event->order->getStatus()); + } +} diff --git a/src/Subscriber/Exception/NotificationHandlerNotRegisteredException.php b/src/Subscriber/Exception/NotificationHandlerNotRegisteredException.php new file mode 100644 index 0000000..2d875a1 --- /dev/null +++ b/src/Subscriber/Exception/NotificationHandlerNotRegisteredException.php @@ -0,0 +1,16 @@ +notificationHandlerType\" is not registered."); + } +} diff --git a/src/Subscriber/Notification/HttpNotificationHandler.php b/src/Subscriber/Notification/HttpNotificationHandler.php new file mode 100644 index 0000000..4a5a990 --- /dev/null +++ b/src/Subscriber/Notification/HttpNotificationHandler.php @@ -0,0 +1,39 @@ + $order->getExternalOrderId(), + 'order_status' => $order->getStatus()->value, + 'response' => $response, + ]; + + $this->httpClient->request($method, $url, [ + 'json' => $data, + ]); + } +} diff --git a/src/Subscriber/Notification/NotificationHandlerCollection.php b/src/Subscriber/Notification/NotificationHandlerCollection.php new file mode 100644 index 0000000..79ac7d7 --- /dev/null +++ b/src/Subscriber/Notification/NotificationHandlerCollection.php @@ -0,0 +1,41 @@ + + */ + private readonly array $handlers; + + public function __construct( + #[TaggedIterator('app.subscriber.notification', indexAttribute: 'type')] + iterable $handlers, + ) { + $this->handlers = iterator_to_array($handlers); + } + + /** + * @param non-empty-string $type + * @throws NotificationHandlerNotRegisteredException + */ + public function getHandler(string $type): NotificationHandlerInterface + { + return $this->handlers[$type] + ?? throw new NotificationHandlerNotRegisteredException($type); + } + + /** + * @return string[] + */ + public function getHandlerTypes(): array + { + return array_keys($this->handlers); + } +} diff --git a/src/Subscriber/Notification/NotificationHandlerInterface.php b/src/Subscriber/Notification/NotificationHandlerInterface.php new file mode 100644 index 0000000..dd12cbf --- /dev/null +++ b/src/Subscriber/Notification/NotificationHandlerInterface.php @@ -0,0 +1,13 @@ +