1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Channel; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface; |
7
|
|
|
use IrishDan\NotificationBundle\Event\MessageCreatedEvent; |
8
|
|
|
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent; |
9
|
|
|
use IrishDan\NotificationBundle\Exception\MessageDispatchException; |
10
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
11
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DirectChannel |
16
|
|
|
* |
17
|
|
|
* @package NotificationBundle\Channel |
18
|
|
|
*/ |
19
|
|
|
class EventChannel extends BaseChannel implements ChannelInterface |
20
|
|
|
{ |
21
|
|
|
private $adapters = []; |
22
|
|
|
private $eventDispatcher; |
23
|
|
|
|
24
|
|
|
public function formatAndDispatch(NotificationInterface $notification) |
25
|
|
|
{ |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function setAdapters($key, MessageAdapterInterface $adapter, array $config) |
30
|
|
|
{ |
31
|
|
|
$adapter->setChannelName($key); |
32
|
|
|
$adapter->setConfiguration($config); |
33
|
|
|
|
34
|
|
|
$this->adapters[$key] = $adapter; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
38
|
|
|
{ |
39
|
|
|
parent::__construct(); |
40
|
|
|
$this->eventDispatcher = $eventDispatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function dispatchFromEvent(MessageCreatedEvent $event) |
44
|
|
|
{ |
45
|
|
|
$message = $event->getMessage(); |
46
|
|
|
$this->dispatch($message); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function dispatch(MessageInterface $message) |
50
|
|
|
{ |
51
|
|
|
$dispatcherKey = $message->getChannel(); |
52
|
|
|
|
53
|
|
|
// Dispatch the message |
54
|
|
|
try { |
55
|
|
|
if (!empty($this->adapters[$dispatcherKey])) { |
56
|
|
|
$this->adapters[$dispatcherKey]->dispatch($message); |
57
|
|
|
|
58
|
|
|
// Dispatch the message event |
59
|
|
|
$messageEvent = new MessageDispatchedEvent($message); |
60
|
|
|
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $messageEvent); |
61
|
|
|
} else { |
62
|
|
|
throw new MessageDispatchException( |
63
|
|
|
sprintf('No adapter available with key "%s"', $dispatcherKey) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} catch (\Exception $exception) { |
67
|
|
|
throw new MessageDispatchException($exception->getMessage()); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|