Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
93.33% covered (success)
93.33%
14 / 15
20.00% covered (danger)
20.00%
2 / 10
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationDispatcherAdapter
100.00% covered (success)
100.00%
10 / 10
93.33% covered (success)
93.33%
14 / 15
20.00% covered (danger)
20.00%
2 / 10
100.00% covered (success)
100.00%
3 / 3
40.77
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
 addSender
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
3 / 3
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
 dispatch
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
8 / 8
0.00% covered (danger)
0.00%
0 / 6
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace App\Notification\Infrastructure\Adapter;
4
5use App\Notification\Domain\Model\Notification;
6use App\Notification\Domain\Model\NotificationDispatchResult;
7use App\Notification\Domain\Port\Driven\NotificationDispatcher;
8use App\Notification\Domain\Port\Driven\NotificationLogRepository;
9use App\Notification\Infrastructure\Sender\NotificationSender;
10use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
11
12/**
13 * @author Wilhelm Zwertvaegher
14 */
15class NotificationDispatcherAdapter implements NotificationDispatcher
16{
17    /**
18     * @var array<NotificationSender>
19     */
20    private array $senders;
21
22    /**
23     * @param iterable<NotificationSender> $senders
24     */
25    public function __construct(
26        #[AutowireIterator('app.notification_sender')]
27        iterable $senders,
28        private readonly NotificationLogRepository $notificationLogRepository
29    ) {
30        $this->senders = is_array($senders) ? $senders : iterator_to_array($senders);
31    }
32
33    public function addSender(NotificationSender $notificationSender): void
34    {
35        if (!array_any($this->senders, fn($sender) => $sender->getName() === $notificationSender->getName())) {
36            $this->senders[] = $notificationSender;
37        }
38    }
39
40    /**
41     * @param Notification $notification
42     * @return array<NotificationDispatchResult>
43     */
44    public function dispatch(Notification $notification): array
45    {
46        $results = [];
47
48        foreach ($this->senders as $sender) {
49            if ($sender->supports($notification) &&
50                // avoid resending a notification which already succeeded
51                // this may be useful in case a notification previously partially failed, which could result in a global retry
52                !$this->notificationLogRepository->hasSuccess($notification->getMessageId(), $sender->getName())
53            ) {
54                $senderResult = $sender->send($notification);
55                $results[] = new NotificationDispatchResult($sender->getName(), $senderResult->getStatus(), $senderResult->getMessage());
56            }
57        }
58
59        return $results;
60    }
61}