Skip to content

Commit

Permalink
#4 describing how to implement custom channels and messages
Browse files Browse the repository at this point in the history
  • Loading branch information
skoro committed Sep 4, 2024
1 parent 63ff43b commit 4585c0d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Subscriber/Channel/ChannelMessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,31 @@
/**
* Channel data message.
*
* A channel uses the message to get (format) the data and send it to a subscriber via notification.
* A channel uses a message to get (format) the data and send it to a subscriber via notification.
*
* Message classes must implement this interface in order be sent via channel.
* Also, it must be tagged as 'app.subscriber.message' with the appropriate type.
*
* For example, a custom message serializes data to JSON:
*
* final class JsonChannelMessage extends AbstractChannelMessage
* {
* public function getData(): string
* {
* return json_encode([
* 'order_num' => $this->getPaymentProcessing()->getOrder()->getExternalOrderId(),
* ]);
* }
* }
*
* In `config/services.yml` the custom channel:
*
* services:
* App\Subscriber\Channel\JsonChannelMessage:
* tags:
* - { name: 'app.subscriber.message', type: 'json' }
*
* Then, the `json` custom message will be available in `subscriber:channels` command output.
*/
interface ChannelMessageInterface
{
Expand Down
29 changes: 29 additions & 0 deletions src/Subscriber/Channel/NotificationChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@
use App\Subscriber\Exception\ChannelMessageException;
use App\Subscriber\Exception\NotificationChannelException;

/**
* Notification channel.
*
* Sends a notification message to a subscriber. A channel is transport for delivering the message.
*
* Custom channel, for example SMS notifications:
*
* final class MyCustomSmsChannel implements NotificationChannelInterface
* {
* private readonly SmsTransport $sms;
*
* // sms transport initialization, so on ...
*
* public function send(ChannelMessageInterface $message, array $params): void
* {
* $this->sms->send($message->getData(), $params['phone_number']);
* }
* }
*
* In `config/services.yml` the custom channel:
*
* services:
* App\Subscriber\Channel\MyCustomSmsChannel:
* tags:
* - { name: 'app.subscriber.channel', type: 'sms' }
*
* Then, the `sms` custom channel will be available in `subscriber:channels` command output under `channels`.
* The adding subscriber command should be implemented too, in order to add subscribers.
*/
interface NotificationChannelInterface
{
/**
Expand Down

0 comments on commit 4585c0d

Please sign in to comment.