Completed
Pull Request — master (#228)
by Beñat
03:51
created

ProjectsResolver::resolveByOrganization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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\Ui\Http\GraphQl\Query\Project;
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\Project\CountProjectsQuery;
21
use Kreta\TaskManager\Application\Query\Project\FilterProjectsQuery;
22
use Kreta\TaskManager\Infrastructure\Ui\Http\GraphQl\Query\Organization\OrganizationResolver;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
25
class ProjectsResolver implements Resolver
26
{
27
    private $connectionBuilder;
28
    private $queryBus;
29
    private $currentUser;
30
    private $organizationResolver;
31
32 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
        TokenStorageInterface $tokenStorage,
34
        ConnectionBuilder $connectionBuilder,
35
        QueryBus $queryBus,
36
        OrganizationResolver $organizationResolver
37
    ) {
38
        $this->connectionBuilder = $connectionBuilder;
39
        $this->queryBus = $queryBus;
40
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
41
        $this->organizationResolver = $organizationResolver;
42
    }
43
44
    public function resolveByOrganization($organizationId, $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...
45
    {
46
        $args['organizationId'] = $organizationId;
47
48
        return $this->resolve($args);
49
    }
50
51
    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...
52
    {
53
        if (!isset($args['name'])) {
54
            $args['name'] = null;
55
        }
56
        if (!isset($args['organizationId'])) {
57
            $args['organizationId'] = null;
58
        }
59
60
        list($offset, $limit, $total) = $this->buildPagination($args);
61
62
        $this->queryBus->handle(
63
            new FilterProjectsQuery(
64
                $this->currentUser,
65
                $offset,
66
                $limit,
67
                $args['organizationId'],
68
                $args['name']
69
            ),
70
            $result
71
        );
72
73
        foreach ($result as $key => $project) {
74
            $result[$key]['organization'] = $this->organizationResolver->resolve([
75
                'id' => $project['organization_id'],
76
            ]);
77
        }
78
79
        $connection = $this->connectionBuilder->fromArraySlice(
80
            $result,
81
            $args,
82
            [
83
                'sliceStart'  => $offset,
84
                'arrayLength' => $total,
85
            ]
86
        );
87
        $connection->totalCount = count($result);
88
89
        return $connection;
90
    }
91
92 View Code Duplication
    private function buildPagination($args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $this->queryBus->handle(
95
            new CountProjectsQuery(
96
                $this->currentUser,
97
                $args['organizationId'],
98
                $args['name']
99
            ),
100
            $total
101
        );
102
103
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
104
            isset($args['before'])
105
                ? $args['before']
106
                : null,
107
            $total
108
        );
109
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
110
            isset($args['after'])
111
                ? $args['after']
112
                : null,
113
            -1
114
        );
115
        $startOffset = max($afterOffset, -1) + 1;
116
        $endOffset = min($beforeOffset, $total);
117
118
        if (isset($args['first']) && is_numeric($args['first'])) {
119
            $endOffset = min($endOffset, $startOffset + $args['first']);
120
        }
121
        if (isset($args['last']) && is_numeric($args['last'])) {
122
            $startOffset = max($startOffset, $endOffset - $args['last']);
123
        }
124
        $offset = max($startOffset, 0);
125
        $limit = $endOffset - $startOffset;
126
127
        return [
128
            $offset,
129
            $limit,
130
            $total,
131
        ];
132
    }
133
}
134