1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Notification; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Notification\Exception\UndeliverableNotificationException; |
18
|
|
|
use Acme\App\Core\Port\Notification\NotificationInterface; |
19
|
|
|
use Acme\App\Core\Port\Notification\NotificationServiceInterface; |
20
|
|
|
use Acme\App\Infrastructure\Notification\Strategy\NotificationStrategyInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Herberto Graca <[email protected]> |
24
|
|
|
* @author Nicolae Nichifor |
25
|
|
|
*/ |
26
|
|
|
final class NotificationService implements NotificationServiceInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var NotificationStrategyInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $notificationStrategyList; |
32
|
|
|
|
33
|
|
|
public function __construct(NotificationStrategyInterface ...$notificationStrategyList) |
34
|
|
|
{ |
35
|
|
|
foreach ($notificationStrategyList as $notificationStrategy) { |
36
|
|
|
$this->notificationStrategyList[$notificationStrategy->getType()->getValue()] = $notificationStrategy; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @throws UndeliverableNotificationException |
42
|
|
|
*/ |
43
|
|
|
public function notify(NotificationInterface $notification): void |
44
|
|
|
{ |
45
|
|
|
foreach ($this->resolveNotificationStrategy($notification) as $notificationStrategyType) { |
46
|
|
|
$this->getNotificationStrategyForType($notificationStrategyType)->notify($notification); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function getNotificationStrategyForType(NotificationType $notificationType): NotificationStrategyInterface |
51
|
|
|
{ |
52
|
|
|
return $this->notificationStrategyList[$notificationType->getValue()]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @throws UndeliverableNotificationException |
57
|
|
|
* |
58
|
|
|
* @return NotificationType[] |
59
|
|
|
*/ |
60
|
|
|
private function resolveNotificationStrategy(NotificationInterface $notification): array |
61
|
|
|
{ |
62
|
|
|
$deliverableBy = \array_map( |
63
|
|
|
function (NotificationStrategyInterface $strategy) { |
64
|
|
|
return $strategy->getType(); |
65
|
|
|
}, |
66
|
|
|
\array_filter( |
67
|
|
|
$this->notificationStrategyList, |
68
|
|
|
function (NotificationStrategyInterface $strategy) use ($notification) { |
69
|
|
|
return $strategy->canHandleNotification($notification); |
70
|
|
|
} |
71
|
|
|
) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
if (empty($deliverableBy)) { |
75
|
|
|
throw new UndeliverableNotificationException( |
76
|
|
|
'Could not find a strategy to deliver the notification ' . \get_class($notification) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $deliverableBy; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|