-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Subscriber/Exception/NotificationHandlerNotRegisteredException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/Subscriber/Notification/NotificationHandlerCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/Subscriber/Notification/NotificationHandlerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |