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

ViewUserNotificationsAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\ViewUserNotificationsQuery;
18
use Kreta\SharedKernel\Application\QueryBus;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
22
23
final class ViewUserNotificationsAction
24
{
25
    private $queryBus;
26
    private $userId;
27
28
    public function __construct(TokenStorageInterface $tokenStorage, QueryBus $queryBus)
29
    {
30
        $this->queryBus = $queryBus;
31
        $this->userId = $tokenStorage->getToken()->getUser()->getUsername();
32
    }
33
34
    public function __invoke(Request $request) : JsonResponse
35
    {
36
        $offset = $request->query->getInt('offset', 0);
37
        $limit = $request->query->getInt('limit', 50);
38
        $status = $request->query->get('status');
39
40
        $this->queryBus->handle(
41
            new ViewUserNotificationsQuery(
42
                $this->userId,
43
                $offset,
44
                $limit,
45
                $status
46
            ),
47
            $notifications
48
        );
49
50
        return new JsonResponse($notifications);
51
    }
52
}
53