Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
13 / 13
36.36% covered (danger)
36.36%
4 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
SendNotificationCommandHandler
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
13 / 13
36.36% covered (danger)
36.36%
4 / 11
100.00% covered (success)
100.00%
2 / 2
19.63
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
12 / 12
30.00% covered (danger)
30.00%
3 / 10
100.00% covered (success)
100.00%
1 / 1
18.35
1<?php
2
3namespace App\Application\Handler;
4
5use App\Enum\NotificationLogStatus;
6use App\Enum\NotificationStatus;
7use App\Exception\NotificationNotFoundException;
8use App\Message\SendNotificationCommand;
9use App\Service\Data\NotificationLogServiceInterface;
10use App\Service\Data\NotificationServiceInterface;
11use App\Service\Notification\Dispatcher\NotificationDispatcherInterface;
12use Doctrine\ORM\EntityManagerInterface;
13
14/**
15 * @author Wilhelm Zwertvaegher
16 */
17readonly class SendNotificationCommandHandler implements SendNotificationCommandHandlerInterface
18{
19    public function __construct(
20        private NotificationServiceInterface $notificationService,
21        private NotificationDispatcherInterface $notificationDispatcher,
22        private NotificationLogServiceInterface $notificationLogService,
23        private EntityManagerInterface $entityManager,
24    ) {
25    }
26
27    /**
28     * @throws NotificationNotFoundException
29     */
30    public function __invoke(SendNotificationCommand $command): void
31    {
32        // get notification to be sent
33        $notification = $this->notificationService->getById($command->getNotificationId());
34        if (null === $notification) {
35            throw new NotificationNotFoundException();
36        }
37
38        if (NotificationStatus::PENDING !== $notification->getStatus()) {
39            return;
40        }
41
42        // pass to dispatcher
43        $results = $this->notificationDispatcher->dispatch($notification);
44        // assume the dispatch is fully successful at first
45        $isSuccess = true;
46
47        // save logs for the notification
48        foreach ($results as $result) {
49            // it at least one dispatch result is not successful, then overall success must be false
50            if (NotificationLogStatus::SENT !== $result->getStatus()) {
51                $isSuccess = false;
52            }
53
54            $this->notificationLogService->createFromNotification(
55                $notification,
56                $result
57            );
58        }
59
60        if ($isSuccess) {
61            $this->notificationService->updateStatus($notification, NotificationStatus::HANDLED);
62        }
63
64        $this->entityManager->flush();
65        // clearing the EM may help with memory consumption
66        $this->entityManager->clear();
67    }
68}