1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Subscriber; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\Adapter\MessageAdapterInterface; |
6
|
|
|
use IrishDan\NotificationBundle\Channel\ChannelInterface; |
7
|
|
|
use IrishDan\NotificationBundle\Channel\EventChannel; |
8
|
|
|
use IrishDan\NotificationBundle\Event\MessageCreatedEvent; |
9
|
|
|
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent; |
10
|
|
|
use IrishDan\NotificationBundle\Event\NotificationReadyToFormatEvent; |
11
|
|
|
use IrishDan\NotificationBundle\Exception\MessageDispatchException; |
12
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class NotificationChannelSubscriber |
19
|
|
|
* |
20
|
|
|
* @package IrishDan\NotificationBundle\Subscriber |
21
|
|
|
*/ |
22
|
|
|
class NotificationChannelSubscriber implements EventSubscriberInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* An array of all available adapters. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $adapters = []; |
30
|
|
|
private $eventDispatcher; |
31
|
|
|
private $adapterlessChannel; |
32
|
|
|
private $channelConfigMap = []; |
33
|
|
|
|
34
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, ChannelInterface $adapterlessChannel, array $channelConfigMap = []) |
35
|
|
|
{ |
36
|
|
|
$this->eventDispatcher = $eventDispatcher; |
37
|
|
|
$this->adapterlessChannel = $adapterlessChannel; |
38
|
|
|
$this->channelConfigMap = $channelConfigMap; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
NotificationReadyToFormatEvent::NAME => 'formatMessageFromEvent' |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param $key |
54
|
|
|
* @param MessageAdapterInterface $adapter |
55
|
|
|
* @param array $config |
56
|
|
|
*/ |
57
|
|
|
public function addAdapter($key, MessageAdapterInterface $adapter, array $config = []) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$adapter->setChannelName($key); |
60
|
|
|
$this->adapters[$key] = $adapter; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
public function formatMessageFromEvent(NotificationReadyToFormatEvent $event) |
65
|
|
|
{ |
66
|
|
|
$notification = $event->getNotification(); |
67
|
|
|
|
68
|
|
|
// get the adapter name from the |
69
|
|
|
// $adapterKey = $message->getChannel(); |
70
|
|
|
$adapterKey = $this->channelConfigMap[$notification->getChannel()]['adapter']; |
71
|
|
|
$channelConfiguration = $this->channelConfigMap[$notification->getChannel()]['config']; |
72
|
|
|
|
73
|
|
|
// Get the adapter for this channel |
74
|
|
|
// and inject it into the adapterless channel |
75
|
|
|
// disable dispatching to events also... |
76
|
|
|
if (!empty($this->adapters[$adapterKey])) { |
77
|
|
|
$adapter = $this->adapters[$adapterKey]; |
78
|
|
|
|
79
|
|
|
// Below could be refactored into a an abstract class for use in a queue worker or else where |
80
|
|
|
$this->adapterlessChannel->setAdapter($adapter); |
81
|
|
|
$this->adapterlessChannel->setChannelConfiguration($channelConfiguration); |
82
|
|
|
|
83
|
|
|
$message = $this->adapterlessChannel->formatAndDispatch($notification, false); |
|
|
|
|
84
|
|
|
// @TODO: Event... |
85
|
|
|
|
86
|
|
|
} else { |
87
|
|
|
throw new MessageDispatchException( |
88
|
|
|
sprintf('No adapter available with key "%s"', $adapterKey) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.