Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

MarkAllAsReadNotificationsAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 6
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B __invoke() 0 27 3
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\Notifier\Infrastructure\Symfony\HttpAction;
16
17
use Kreta\Notifier\Application\Inbox\Notification\MarkAsReadNotificationCommand;
18
use Kreta\Notifier\Application\Inbox\Notification\ViewUserNotificationsQuery;
19
use Kreta\Notifier\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
20
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
21
use Kreta\SharedKernel\Application\CommandBus;
22
use Kreta\SharedKernel\Application\QueryBus;
23
use Symfony\Component\HttpFoundation\JsonResponse;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
26
final class MarkAllAsReadNotificationsAction
27
{
28
    private $commandBus;
29
    private $userId;
30
    private $queryBus;
31
32
    public function __construct(TokenStorageInterface $tokenStorage, CommandBus $commandBus, QueryBus $queryBus)
33
    {
34
        $this->commandBus = $commandBus;
35
        $this->userId = $tokenStorage->getToken()->getUser()->getUsername();
36
        $this->queryBus = $queryBus;
37
    }
38
39
    public function __invoke() : JsonResponse
40
    {
41
        $this->queryBus->handle(
42
            new ViewUserNotificationsQuery(
43
                $this->userId,
44
                0,
45
                -1,
46
                'unread'
47
            ),
48
            $notifications
49
        );
50
51
        try {
52
            foreach ($notifications as $notification) {
53
                $this->commandBus->handle(
54
                    new MarkAsReadNotificationCommand(
55
                        $notification->id,
56
                        $this->userId
57
                    )
58
                );
59
            }
60
        } catch (UserDoesNotExist | NotificationDoesNotExist $exception) {
61
            return new JsonResponse(null, 404);
62
        }
63
64
        return new JsonResponse();
65
    }
66
}
67