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 setDispatchers($key, MessageDispatcherInterface $dispatcher) |
30
|
|
|
{ |
31
|
|
|
$this->dispatchers[$key] = $dispatcher; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher) |
35
|
|
|
{ |
36
|
|
|
// @TODO: Configured, for what?? |
|
|
|
|
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 $exception) { |
48
|
|
|
throw new MessageFormatException( |
49
|
|
|
$exception->getMessage() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Dispatch the message event |
54
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
55
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
56
|
|
|
|
57
|
|
|
return $message; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function dispatchFromEvent(MessageCreatedEvent $event) |
61
|
|
|
{ |
62
|
|
|
$message = $event->getMessage(); |
63
|
|
|
$this->dispatch($message); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function dispatch(MessageInterface $message) |
67
|
|
|
{ |
68
|
|
|
// @TODO: need to figure out which channel message was supposed to be dispatched on. |
69
|
|
|
$dispatcherKey = $message->getChannel(); |
70
|
|
|
|
71
|
|
|
// Dispatch the message |
72
|
|
|
try { |
73
|
|
|
if (!empty($this->dispatchers[$dispatcherKey])) { |
74
|
|
|
$this->dispatchers[$dispatcherKey]->dispatch($message); |
75
|
|
|
} |
76
|
|
|
else { |
77
|
|
|
throw new MessageDispatchException( |
78
|
|
|
sprintf('No dispatcher available with key "%s"', $dispatcherKey) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} catch (\Exception $exception) { |
82
|
|
|
throw new MessageDispatchException($exception->getMessage()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Dispatch the message event |
86
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
87
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.