|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* Copyright (C) |
|
6
|
|
|
* Nathan Boiron <[email protected]> |
|
7
|
|
|
* Romain Canon <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This file is part of the TYPO3 NotiZ project. |
|
10
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
11
|
|
|
* under the terms of the GNU General Public License, either |
|
12
|
|
|
* version 3 of the License, or any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, see: |
|
15
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace CuyZ\Notiz\Core\Notification; |
|
19
|
|
|
|
|
20
|
|
|
use Closure; |
|
21
|
|
|
use CuyZ\Notiz\Core\Channel\Payload; |
|
22
|
|
|
use CuyZ\Notiz\Core\Channel\Service\ChannelFactory; |
|
23
|
|
|
use CuyZ\Notiz\Core\Definition\DefinitionService; |
|
24
|
|
|
use CuyZ\Notiz\Core\Definition\Tree\Definition; |
|
25
|
|
|
use CuyZ\Notiz\Core\Definition\Tree\EventGroup\Event\EventDefinition; |
|
26
|
|
|
use CuyZ\Notiz\Core\Definition\Tree\Notification\Channel\ChannelDefinition; |
|
27
|
|
|
use CuyZ\Notiz\Core\Definition\Tree\Notification\NotificationDefinition; |
|
28
|
|
|
use CuyZ\Notiz\Core\Event\Event; |
|
29
|
|
|
use CuyZ\Notiz\Core\Notification\Container\NotificationContainer; |
|
30
|
|
|
use Generator; |
|
31
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
32
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
33
|
|
|
|
|
34
|
|
|
class NotificationDispatcher implements SingletonInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Definition |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $definition; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ChannelFactory |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $channelFactory; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param DefinitionService $definitionService |
|
48
|
|
|
* @param ChannelFactory $channelFactory |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(DefinitionService $definitionService, ChannelFactory $channelFactory) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->definition = $definitionService->getDefinition(); |
|
53
|
|
|
$this->channelFactory = $channelFactory; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Fetches all notifications bound to the given event to dispatch them in |
|
58
|
|
|
* every registered channel. |
|
59
|
|
|
* |
|
60
|
|
|
* @param EventDefinition $eventDefinition |
|
61
|
|
|
* @return Generator |
|
62
|
|
|
*/ |
|
63
|
|
|
public function fetchNotifications(EventDefinition $eventDefinition): Generator |
|
64
|
|
|
{ |
|
65
|
|
|
$notificationTypes = $this->definition->getNotifications(); |
|
66
|
|
|
|
|
67
|
|
|
// Looping on all existing notification types... |
|
68
|
|
|
foreach ($notificationTypes as $notificationDefinition) { |
|
69
|
|
|
// ...to filter the ones bound to this specific event definition. |
|
70
|
|
|
$notifications = $this->getNotificationContainer($notificationDefinition)->fetchFromEventDefinition($eventDefinition); |
|
71
|
|
|
|
|
72
|
|
|
foreach ($notifications as $notification) { |
|
73
|
|
|
yield $notification => $this->getDispatchCallback($notification, $notificationDefinition); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param Notification $notification |
|
80
|
|
|
* @param NotificationDefinition $notificationDefinition |
|
81
|
|
|
* @return Closure |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getDispatchCallback(Notification $notification, NotificationDefinition $notificationDefinition): Closure |
|
84
|
|
|
{ |
|
85
|
|
|
return function (Event $event) use ($notification, $notificationDefinition) { |
|
86
|
|
|
/** @var Payload $payload */ |
|
87
|
|
|
$payload = GeneralUtility::makeInstance(Payload::class, $notification, $notificationDefinition, $event); |
|
88
|
|
|
|
|
89
|
|
|
$channels = $this->getChannels($notification, $notificationDefinition); |
|
90
|
|
|
|
|
91
|
|
|
foreach ($channels as $channelDefinition) { |
|
92
|
|
|
$channel = $this->channelFactory->create($channelDefinition); |
|
93
|
|
|
|
|
94
|
|
|
$channel->dispatch($payload); |
|
95
|
|
|
|
|
96
|
|
|
unset($channel); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
unset($payload); |
|
100
|
|
|
}; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param NotificationDefinition $notificationDefinition |
|
105
|
|
|
* @return NotificationContainer |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function getNotificationContainer(NotificationDefinition $notificationDefinition): NotificationContainer |
|
108
|
|
|
{ |
|
109
|
|
|
/** @var NotificationContainer $notificationContainer */ |
|
110
|
|
|
$notificationContainer = GeneralUtility::makeInstance(NotificationContainer::class, $notificationDefinition); |
|
111
|
|
|
|
|
112
|
|
|
return $notificationContainer; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* This method is responsible for returning the list of channels to dispatch |
|
117
|
|
|
* for the given notification (and its definition). |
|
118
|
|
|
* |
|
119
|
|
|
* @param Notification $notification |
|
120
|
|
|
* @param NotificationDefinition $definition |
|
121
|
|
|
* @return ChannelDefinition[] |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getChannels(Notification $notification, NotificationDefinition $definition): array |
|
124
|
|
|
{ |
|
125
|
|
|
if ($notification instanceof MultipleChannelsNotification) { |
|
126
|
|
|
/* |
|
127
|
|
|
* If a notification is supported by several channels, it can choose |
|
128
|
|
|
* which ones to dispatch. |
|
129
|
|
|
*/ |
|
130
|
|
|
$channels = array_filter( |
|
131
|
|
|
$definition->getChannels(), |
|
132
|
|
|
[$notification, 'shouldDispatch'] |
|
133
|
|
|
); |
|
134
|
|
|
} else { |
|
135
|
|
|
$channels = $definition->getChannels(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $channels; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|