1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Channel; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Event\MessageCreatedEvent; |
7
|
|
|
use IrishDan\NotificationBundle\Exception\MessageDispatchException; |
8
|
|
|
use IrishDan\NotificationBundle\Exception\MessageFormatException; |
9
|
|
|
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface; |
10
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
11
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DefaultChannel |
16
|
|
|
* |
17
|
|
|
* @package NotificationBundle\Channel |
18
|
|
|
*/ |
19
|
|
|
class EventChannel extends BaseChannel implements ChannelInterface |
20
|
|
|
{ |
21
|
|
|
private $dispatchers = []; |
|
|
|
|
22
|
|
|
private $eventDispatcher; |
23
|
|
|
|
24
|
|
|
public function formatAndDispatch(NotificationInterface $notification) |
25
|
|
|
{ |
26
|
|
|
$this->format($notification); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __construct($configured = false, $channel = 'default', EventDispatcherInterface $eventDispatcher) |
30
|
|
|
{ |
31
|
|
|
$this->configured = $configured; |
|
|
|
|
32
|
|
|
$this->channel = $channel; |
33
|
|
|
$this->eventDispatcher = $eventDispatcher; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function format(NotificationInterface $notification) |
37
|
|
|
{ |
38
|
|
|
try { |
39
|
|
|
// Do the formatting. |
40
|
|
|
$message = $this->formatter->format($notification); |
41
|
|
|
} catch (\Exception $e) { |
42
|
|
|
throw new MessageFormatException(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// Dispatch the message event |
46
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
47
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function dispatchFromEvent(MessageCreatedEvent $event) |
51
|
|
|
{ |
52
|
|
|
$message = $event->getMessage(); |
53
|
|
|
$this->dispatch($message); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function dispatch(MessageInterface $message) |
57
|
|
|
{ |
58
|
|
|
// @TODO: need to figure out which channel message was supposed to be dispatched on. |
59
|
|
|
$dispatcherKey = $message->getChannel(); |
|
|
|
|
60
|
|
|
|
61
|
|
|
// Dispatch the message |
62
|
|
|
try { |
|
|
|
|
63
|
|
|
|
64
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
65
|
|
|
throw new MessageDispatchException(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.