| @@ 9-36 (lines=28) @@ | ||
| 6 | use IrishDan\NotificationBundle\Exception\MessageFormatException; |
|
| 7 | use IrishDan\NotificationBundle\Notification\NotificationInterface; |
|
| 8 | ||
| 9 | class DatabaseDataFormatter extends BaseFormatter implements MessageFormatterInterface |
|
| 10 | { |
|
| 11 | const CHANNEL = 'database'; |
|
| 12 | ||
| 13 | public function format(NotificationInterface $notification) |
|
| 14 | { |
|
| 15 | $notification->setChannel(self::CHANNEL); |
|
| 16 | parent::format($notification); |
|
| 17 | ||
| 18 | // /** @var DatabaseNotifiableInterface $notifiable */ |
|
| 19 | $notifiable = $notification->getNotifiable(); |
|
| 20 | if (!$notifiable instanceof DatabaseNotifiableInterface) { |
|
| 21 | throw new MessageFormatException( |
|
| 22 | 'Notifiable must implement DatabaseNotifiableInterface interface in order to format email message' |
|
| 23 | ); |
|
| 24 | } |
|
| 25 | ||
| 26 | // Build the dispatch data array. |
|
| 27 | $dispatchData = [ |
|
| 28 | 'id' => $notifiable->getId(), |
|
| 29 | ]; |
|
| 30 | ||
| 31 | $messageData = self::createMessagaData($notification->getDataArray()); |
|
| 32 | $message = self::createMessage($dispatchData, $messageData, self::CHANNEL); |
|
| 33 | ||
| 34 | return $message; |
|
| 35 | } |
|
| 36 | } |
|
| @@ 9-36 (lines=28) @@ | ||
| 6 | use IrishDan\NotificationBundle\Notification\NotificationInterface; |
|
| 7 | use IrishDan\NotificationBundle\SlackableInterface; |
|
| 8 | ||
| 9 | class SlackWebhookFormatter extends BaseFormatter implements MessageFormatterInterface |
|
| 10 | { |
|
| 11 | const CHANNEL = 'slack'; |
|
| 12 | ||
| 13 | public function format(NotificationInterface $notification) |
|
| 14 | { |
|
| 15 | $notification->setChannel(self::CHANNEL); |
|
| 16 | parent::format($notification); |
|
| 17 | ||
| 18 | /** @var SlackableInterface $notifiable */ |
|
| 19 | $notifiable = $notification->getNotifiable(); |
|
| 20 | if (!$notifiable instanceof SlackableInterface) { |
|
| 21 | throw new MessageFormatException( |
|
| 22 | 'Notifiable must implement SlackableInterface interface in order to format email message' |
|
| 23 | ); |
|
| 24 | } |
|
| 25 | ||
| 26 | // Build the dispatch data array. |
|
| 27 | $dispatchData = [ |
|
| 28 | 'webhook' => $notifiable->getSlackWebhook(), |
|
| 29 | ]; |
|
| 30 | ||
| 31 | $messageData = self::createMessagaData($notification->getDataArray()); |
|
| 32 | $message = self::createMessage($dispatchData, $messageData, self::CHANNEL); |
|
| 33 | ||
| 34 | return $message; |
|
| 35 | } |
|
| 36 | } |
|