Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Adapter/BaseMessageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ protected function renderTwigTemplate($data, $user, $template)
*/
public function format(NotificationInterface $notification)
{
$notification->setChannel($this->channelName);
// @TODO: $this->channelName
// $notification->setChannel($this->channelName);

$templateArray = $notification->getTemplateArray();
if (!empty($this->twig) && !empty($templateArray)) {
Expand Down
9 changes: 7 additions & 2 deletions Adapter/DatabaseMessageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ public function __construct(DatabaseNotificationManager $databaseNotificationMan
}

/**
* Generates a message object
* Generates a Message object
*
* @param NotificationInterface $notification
* @return \IrishDan\NotificationBundle\Message\Message
*
* @return \IrishDan\NotificationBundle\Message\Message|MessageInterface|void
*
* @throws \IrishDan\NotificationBundle\Exception\MessageFormatException
*/
public function format(NotificationInterface $notification)
{
Expand Down Expand Up @@ -57,6 +60,8 @@ public function dispatch(MessageInterface $message)
return true;
}

// @todo: return something use full or throw exception is better

return false;
}
}
8 changes: 5 additions & 3 deletions Adapter/MailMessageAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(\Swift_Mailer $mailer)
*/
public function format(NotificationInterface $notification)
{
$notification->setChannel($this->channelName);
// $notification->setChannel($this->channelName);
parent::format($notification);

/** @var EmailableInterface $notifiable */
Expand Down Expand Up @@ -75,8 +75,10 @@ public function dispatch(MessageInterface $message)
$dispatchData = $message->getDispatchData();
$messageData = $message->getMessageData();

$mail = \Swift_Message::newInstance()
->setSubject($messageData['title'])
// has been remocved in swiftmailer 6.0.0,
// @TODO updatebundle depenedencies to reflex this.
// $mail = \Swift_Message::newInstance()
$mail = (new \Swift_Message($messageData['title']))
->setBody($messageData['body']);

$mail->setFrom($dispatchData['from']);
Expand Down
2 changes: 1 addition & 1 deletion Broadcast/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function broadcast(NotificationInterface $notification)
$notification->setChannel($this->channelName);

// Format and send the broadcast.
$this->channel->setDispatchToEvent(false);
$this->channel->setDispatchAsEvent(false);
$this->channel->formatAndDispatch($notification);
}
}
22 changes: 22 additions & 0 deletions Channel/BaseChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ abstract class BaseChannel implements ChannelInterface
public function __construct(array $channelConfiguration = [], $channelName = null, MessageAdapterInterface $adapter = null)
{
$this->channelConfiguration = $channelConfiguration;

$this->channelName = $channelName;

if (!empty($adapter)) {
Expand All @@ -38,6 +39,27 @@ public function __construct(array $channelConfiguration = [], $channelName = nul
}
}

/**
* @param array $channelConfiguration
*/
public function setChannelConfiguration(array $channelConfiguration): void
{
$this->channelConfiguration = $channelConfiguration;
}

/**
* @param mixed $channelName
*/
public function setChannelName(string $channelName): void
{
$this->channelName = $channelName;
}

public function setAdapter(MessageAdapterInterface $adapter): void
{
$this->adapter = $adapter;
}

/**
* @return mixed
*/
Expand Down
62 changes: 48 additions & 14 deletions Channel/DirectChannel.php → Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent;
use IrishDan\NotificationBundle\Event\NotificationReadyToFormatEvent;
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
use IrishDan\NotificationBundle\Exception\MessageFormatException;
use IrishDan\NotificationBundle\Message\MessageInterface;
Expand All @@ -12,40 +13,68 @@


/**
* Class DirectChannel
* Class Channel
*
* @package NotificationBundle\Channel
*/
class DirectChannel extends BaseChannel implements ChannelInterface
class Channel extends BaseChannel implements ChannelInterface
{
/**
* @var
*/
protected $eventDispatcher;
protected $dispatchToEvent = true;
protected $formatToEvent = false;
protected $dispatchToEvent = false;

public function setDispatchToEvent($dispatchToEvent)
/**
* @param $dispatchToEvent
*/
public function setDispatchAsEvent(bool $dispatchToEvent): void
{
$this->dispatchToEvent = $dispatchToEvent;
}

/**
* @param $dispatchToEvent
*/
public function setFormatAsEvent(bool $formatToEvent): void
{
$this->formatToEvent = $formatToEvent;
}

/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

public function formatAndDispatch(NotificationInterface $notification)
public function formatAndDispatch(NotificationInterface $notification, $dispatchReadyEvent = true)
{
// Channels can be configured to format and dispatch either directly..
// or via events..
// Using events allows for the hooking into the formatting and dispatching..
// process, perhaps t offload these process to a queue worker.
if ($this->dispatchToEvent && $dispatchReadyEvent) {
$readyEvent = new NotificationReadyToFormatEvent($notification);
$this->eventDispatcher->dispatch(NotificationReadyToFormatEvent::NAME, $readyEvent);

return;
}

// Format the message..
// Creates a message object for each recipient
// If twig enabled will use twig to render the message
$message = $this->format($notification);

if (!empty($this->eventDispatcher) && $this->dispatchToEvent) {
$messageEvent = new MessageCreatedEvent($message);
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
// Dispatch the message..
// Sends the message to the destination..
$this->dispatch($message);

return true;
} else {
return $this->dispatch($message);
}
$message->setStatus('sent');

return $message;
}

public function format(NotificationInterface $notification)
Expand All @@ -54,6 +83,11 @@ public function format(NotificationInterface $notification)
// Do the formatting.
$message = $this->adapter->format($notification);

if (!empty($this->eventDispatcher)) {
$messageEvent = new MessageCreatedEvent($message);
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
}

return $message;
} catch (\Exception $e) {
throw new MessageFormatException(
Expand All @@ -69,8 +103,8 @@ public function dispatch(MessageInterface $message)
$sent = $this->adapter->dispatch($message);

if ($sent && !empty($this->eventDispatcher)) {
$event = new MessageDispatchedEvent($message);
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $event);
$messageEvent = new MessageDispatchedEvent($message);
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $messageEvent);
}

return $sent;
Expand Down
9 changes: 8 additions & 1 deletion Channel/ChannelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace IrishDan\NotificationBundle\Channel;

use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface;
use IrishDan\NotificationBundle\Message\MessageInterface;
use IrishDan\NotificationBundle\Notification\NotificationInterface;

Expand All @@ -19,7 +20,13 @@ interface ChannelInterface
* @param NotificationInterface $notification
* @return boolean
*/
public function formatAndDispatch(NotificationInterface $notification);
public function formatAndDispatch(NotificationInterface $notification, $dispatchReadyEvent = true);

public function channelName();

public function setAdapter(MessageAdapterInterface $adapter): void;

public function setChannelName(string $channelName): void;

public function setChannelConfiguration(array $config): void;
}
60 changes: 22 additions & 38 deletions Channel/EventChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,43 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class DirectChannel
* Class EventChannel
*
* This type of channel dispatched events when each Message
* is created..
* and when each Message is dispatched
*
* @package NotificationBundle\Channel
*/
class EventChannel extends BaseChannel implements ChannelInterface
{
/**
* An array of all available adapters.
*
* @var array
*/
private $adapters = [];
private $eventDispatcher;

public function formatAndDispatch(NotificationInterface $notification)
{
return false;
}

/**
* @param $key
* @param MessageAdapterInterface $adapter
* @param array $config
*/
public function setAdapters($key, MessageAdapterInterface $adapter, array $config)
{
$adapter->setChannelName($key);
// $adapter->setChannelName($key);
$adapter->setConfiguration($config);

$this->adapters[$key] = $adapter;
}

public function __construct(EventDispatcherInterface $eventDispatcher)
{
parent::__construct();
$this->eventDispatcher = $eventDispatcher;
}

public function dispatchFromEvent(MessageCreatedEvent $event)
{
$message = $event->getMessage();
$this->dispatch($message);
}

public function dispatch(MessageInterface $message)
/**
* @param NotificationInterface $notification
* @return bool
*/
public function formatAndDispatch(NotificationInterface $notification)
{
$dispatcherKey = $message->getChannel();

// Dispatch the message
try {
if (!empty($this->adapters[$dispatcherKey])) {
$this->adapters[$dispatcherKey]->dispatch($message);

// Dispatch the message event
$messageEvent = new MessageDispatchedEvent($message);
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $messageEvent);
} else {
throw new MessageDispatchException(
sprintf('No adapter available with key "%s"', $dispatcherKey)
);
}
} catch (\Exception $exception) {
throw new MessageDispatchException($exception->getMessage());
}
return false;
}
}
17 changes: 9 additions & 8 deletions ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace IrishDan\NotificationBundle;


use IrishDan\NotificationBundle\Channel\ChannelInterface;
use IrishDan\NotificationBundle\Event\NotificationFailedEvent;
use IrishDan\NotificationBundle\Event\NotificationSentEvent;
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
use IrishDan\NotificationBundle\Notification\NotificationInterface;
use IrishDan\NotificationBundle\Event\NotificationSendingEvent;
use Ramsey\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -60,10 +60,10 @@ public function sendNow(array $recipients, NotificationInterface $notification)

// Set a uuid so notifications from different channels can be grouped.
// Needed when marking as read across all channels.
$uuid = uniqid();
$uuid = Uuid::uuid4();

foreach ($recipients as $notifiable) {
// Get all of the channels the notification would like to be send on.
// Get all of the channels the notification would like to be sent on.
// Then check each channel against what is configured in the system,
// and which channels the notifiable is subscribed to.
$viaChannels = $notification->getChannels();
Expand All @@ -77,8 +77,8 @@ public function sendNow(array $recipients, NotificationInterface $notification)

try {
// Dispatch sending event.
$sendingEvent = new NotificationSendingEvent($currentNotification);
$this->eventDispatcher->dispatch(NotificationSendingEvent::NAME, $sendingEvent);
// $sendingEvent = new NotificationSendingEvent($currentNotification);
// $this->eventDispatcher->dispatch(NotificationSendingEvent::NAME, $sendingEvent);

$response = $this->channels[$channel]->formatAndDispatch($currentNotification);

Expand Down Expand Up @@ -110,13 +110,14 @@ public function sendNow(array $recipients, NotificationInterface $notification)
protected function shouldSendNotification(NotifiableInterface $notifiable, NotificationInterface $notification, $channel)
{
$notifiableChannels = $notifiable->getSubscribedChannels();
$configuredChannels = $notification->getChannels();
$notificationChannels = $notification->getChannels();

if (
in_array($channel, $configuredChannels)
in_array($channel, $notificationChannels)
&& in_array($channel, $notifiableChannels)
//@TODO: Sometimes its 'mail' other times its 'email', check the maker command.
&& in_array($channel, $this->configuredChannels)
&& in_array($channel, array_keys($this->channels))
// && in_array($channel, array_keys($this->channels))
) {
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions Command/CreateDatabaseNotificationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace IrishDan\NotificationBundle\Command;

use IrishDan\NotificationBundle\Generator\DatabaseNotificationGenerator;
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -12,7 +12,8 @@
use Symfony\Component\Console\Style\SymfonyStyle;


class CreateDatabaseNotificationCommand extends GeneratorCommand
// class CreateDatabaseNotificationCommand extends GeneratorCommand
class CreateDatabaseNotificationCommand extends Command
{
private $entityName;

Expand Down
Loading