OrganizationsResolver::buildPagination()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 5
eloc 26
nc 4
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\TaskManager\Infrastructure\Symfony\GraphQl\Query\Organization;
16
17
use Kreta\SharedKernel\Application\QueryBus;
18
use Kreta\SharedKernel\Http\GraphQl\Relay\ConnectionBuilder;
19
use Kreta\SharedKernel\Http\GraphQl\Resolver;
20
use Kreta\TaskManager\Application\Query\Organization\CountOrganizationsQuery;
21
use Kreta\TaskManager\Application\Query\Organization\FilterOrganizationsQuery;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
24
class OrganizationsResolver implements Resolver
25
{
26
    private $connectionBuilder;
27
    private $queryBus;
28
    private $currentUser;
29
30
    public function __construct(
31
        TokenStorageInterface $tokenStorage,
32
        ConnectionBuilder $connectionBuilder,
33
        QueryBus $queryBus
34
    ) {
35
        $this->connectionBuilder = $connectionBuilder;
36
        $this->queryBus = $queryBus;
37
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
38
    }
39
40
    public function resolve($args)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
41
    {
42
        if (!isset($args['name'])) {
43
            $args['name'] = null;
44
        }
45
        if (isset($args['organizationConnectionInput'])) {
46
            foreach ($args['organizationConnectionInput'] as $key => $value) {
47
                $args[$key] = $value;
48
            }
49
            unset($args['organizationConnectionInput']);
50
        }
51
52
        list($offset, $limit, $total) = $this->buildPagination($args);
53
54
        $this->queryBus->handle(
55
            new FilterOrganizationsQuery(
56
                $this->currentUser,
57
                $offset,
58
                $limit,
59
                $args['name']
60
            ),
61
            $result
62
        );
63
64
        $connection = $this->connectionBuilder->fromArraySlice(
65
            $result,
66
            $args,
67
            [
68
                'sliceStart'  => $offset,
69
                'arrayLength' => $total,
70
            ]
71
        );
72
        $connection->totalCount = count($result);
73
74
        return $connection;
75
    }
76
77
    private function buildPagination($args)
78
    {
79
        $this->queryBus->handle(
80
            new CountOrganizationsQuery(
81
                $this->currentUser,
82
                $args['name']
83
            ),
84
            $total
85
        );
86
87
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
88
            $args['before']
89
                ?? null,
90
            $total
91
        );
92
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
93
            $args['after']
94
                ?? null,
95
            -1
96
        );
97
        $startOffset = max($afterOffset, -1) + 1;
98
        $endOffset = min($beforeOffset, $total);
99
100
        if (isset($args['first']) && is_numeric($args['first'])) {
101
            $endOffset = min($endOffset, $startOffset + $args['first']);
102
        }
103
        if (isset($args['last']) && is_numeric($args['last'])) {
104
            $startOffset = max($startOffset, $endOffset - $args['last']);
105
        }
106
        $offset = max($startOffset, 0);
107
        $limit = $endOffset - $startOffset;
108
109
        return [
110
            $offset,
111
            $limit,
112
            $total,
113
        ];
114
    }
115
}
116