MarkAsReadNotificationAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 15 2
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\Domain\Model\Inbox\Notification\NotificationDoesNotExist;
19
use Kreta\Notifier\Domain\Model\Inbox\UserDoesNotExist;
20
use Kreta\SharedKernel\Application\CommandBus;
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
24
final class MarkAsReadNotificationAction
25
{
26
    private $commandBus;
27
    private $userId;
28
29
    public function __construct(TokenStorageInterface $tokenStorage, CommandBus $commandBus)
30
    {
31
        $this->commandBus = $commandBus;
32
        $this->userId = $tokenStorage->getToken()->getUser()->getUsername();
33
    }
34
35
    public function __invoke(string $notificationId) : JsonResponse
36
    {
37
        try {
38
            $this->commandBus->handle(
39
                new MarkAsReadNotificationCommand(
40
                    $notificationId,
41
                    $this->userId
42
                )
43
            );
44
        } catch (UserDoesNotExist | NotificationDoesNotExist $exception) {
45
            return new JsonResponse(null, 404);
46
        }
47
48
        return new JsonResponse();
49
    }
50
}
51