Skip to content

Commit

Permalink
#4 subscriber notifications, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
skoro committed Aug 31, 2024
1 parent d901eb7 commit 0c5f8a3
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ services:
App\Order\OrderTotalAmountCalculator:
arguments:
$currencyCode: 'UAH'

App\Subscriber\Notification\HttpNotificationHandler:
tags:
- { name: 'app.subscriber.notification', type: 'http' }
23 changes: 23 additions & 0 deletions src/EventListener/NotifySubscribersListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace App\EventListener;

use App\Event\OrderStatusWasChanged;
use App\Repository\SubscriberRepository;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

final class NotifySubscribersListener
{
public function __construct(
private readonly SubscriberRepository $subscriberRepository,
) {
}

#[AsEventListener(event: OrderStatusWasChanged::class)]
public function onOrderStatusWasChanged(OrderStatusWasChanged $event): void
{
$subscribers = $this->subscriberRepository->getList($event->order->getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Subscriber\Exception;

use Exception;

final class NotificationHandlerNotRegisteredException extends Exception
{
public function __construct(
public readonly string $notificationHandlerType,
) {
parent::__construct("Subscriber notification handler type \"$this->notificationHandlerType\" is not registered.");
}
}
39 changes: 39 additions & 0 deletions src/Subscriber/Notification/HttpNotificationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Subscriber\Notification;

use App\Entity\Order;
use App\Payment\Common\Message\ResponseInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final readonly class HttpNotificationHandler implements NotificationHandlerInterface
{
public function __construct(
private HttpClientInterface $httpClient,
) {
}

/**
* @param Order $order
* @param ResponseInterface $response
* @param array{url: string, method: string} $params
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function send(Order $order, ResponseInterface $response, array $params): void
{
$url = $params['url'] ?? '';
$method = $params['method'] ?? '';

$data = [
'order_num' => $order->getExternalOrderId(),
'order_status' => $order->getStatus()->value,
'response' => $response,
];

$this->httpClient->request($method, $url, [
'json' => $data,
]);
}
}
41 changes: 41 additions & 0 deletions src/Subscriber/Notification/NotificationHandlerCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Subscriber\Notification;

use App\Subscriber\Exception\NotificationHandlerNotRegisteredException;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

class NotificationHandlerCollection
{
/**
* @var array<string, NotificationHandlerInterface>
*/
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);
}
}
13 changes: 13 additions & 0 deletions src/Subscriber/Notification/NotificationHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Subscriber\Notification;

use App\Entity\Order;
use App\Payment\Common\Message\ResponseInterface;

interface NotificationHandlerInterface
{
public function send(Order $order, ResponseInterface $response, array $params): void;
}

0 comments on commit 0c5f8a3

Please sign in to comment.