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

MarkAsUnreadNotificationAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
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\MarkAsUnreadNotificationCommand;
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 MarkAsUnreadNotificationAction
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 MarkAsUnreadNotificationCommand(
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