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 implements ChannelInterface |
20
|
|
|
{ |
21
|
|
|
private $formatter; |
22
|
|
|
private $dispatchers = []; |
23
|
|
|
private $eventDispatcher; |
24
|
|
|
|
25
|
|
|
public function setDispatchers($dispatcherKey, MessageDispatcherInterface $dispatcher) |
26
|
|
|
{ |
27
|
|
|
$this->dispatchers[$dispatcherKey] = $dispatcher; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setDataFormatter(MessageFormatterInterface $formatter) |
31
|
|
|
{ |
32
|
|
|
$this->formatter = $formatter; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __construct($configured = false, $channel = 'default', EventDispatcherInterface $eventDispatcher) |
36
|
|
|
{ |
37
|
|
|
$this->configured = $configured; |
|
|
|
|
38
|
|
|
$this->channel = $channel; |
|
|
|
|
39
|
|
|
$this->eventDispatcher = $eventDispatcher; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function format(NotificationInterface $notification) |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
// Do the formatting. |
46
|
|
|
$message = $this->formatter->format($notification); |
47
|
|
|
} catch (\Exception $e) { |
48
|
|
|
throw new MessageFormatException(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Dispatch the message event |
52
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
53
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function dispatchFromEvent(MessageCreatedEvent $event) |
57
|
|
|
{ |
58
|
|
|
$message = $event->getMessage(); |
59
|
|
|
$this->dispatch($message); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function dispatch(MessageInterface $message) |
63
|
|
|
{ |
64
|
|
|
// @TODO: need to figure out which channel message was supposed to be dispatched on. |
65
|
|
|
$dispatcherKey = $message->getChannel(); |
|
|
|
|
66
|
|
|
|
67
|
|
|
// Dispatch the message |
68
|
|
|
try { |
|
|
|
|
69
|
|
|
|
70
|
|
|
} catch (\Exception $exception) { |
|
|
|
|
71
|
|
|
throw new MessageDispatchException(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: