ProjectsResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 113
Duplicated Lines 9.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 11
loc 113
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
A resolveByOrganization() 0 6 1
B resolve() 0 46 6
B buildPagination() 0 39 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\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\Symfony\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
        if (isset($args['projectConnectionInput'])) {
60
            foreach ($args['projectConnectionInput'] as $key => $value) {
61
                $args[$key] = $value;
62
            }
63
            unset($args['projectConnectionInput']);
64
        }
65
66
        list($offset, $limit, $total) = $this->buildPagination($args);
67
68
        $this->queryBus->handle(
69
            new FilterProjectsQuery(
70
                $this->currentUser,
71
                $offset,
72
                $limit,
73
                $args['organizationId'],
74
                $args['name']
75
            ),
76
            $result
77
        );
78
79
        foreach ($result as $key => $project) {
80
            $result[$key]['organization'] = $this->organizationResolver->resolve([
81
                'id' => $project['organization_id'],
82
            ]);
83
        }
84
85
        $connection = $this->connectionBuilder->fromArraySlice(
86
            $result,
87
            $args,
88
            [
89
                'sliceStart'  => $offset,
90
                'arrayLength' => $total,
91
            ]
92
        );
93
        $connection->totalCount = count($result);
94
95
        return $connection;
96
    }
97
98
    private function buildPagination($args)
99
    {
100
        $this->queryBus->handle(
101
            new CountProjectsQuery(
102
                $this->currentUser,
103
                $args['organizationId'],
104
                $args['name']
105
            ),
106
            $total
107
        );
108
109
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
110
            $args['before']
111
            ?? null,
112
            $total
113
        );
114
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
115
            $args['after']
116
            ?? null,
117
            -1
118
        );
119
        $startOffset = max($afterOffset, -1) + 1;
120
        $endOffset = min($beforeOffset, $total);
121
122
        if (isset($args['first']) && is_numeric($args['first'])) {
123
            $endOffset = min($endOffset, $startOffset + $args['first']);
124
        }
125
        if (isset($args['last']) && is_numeric($args['last'])) {
126
            $startOffset = max($startOffset, $endOffset - $args['last']);
127
        }
128
        $offset = max($startOffset, 0);
129
        $limit = $endOffset - $startOffset;
130
131
        return [
132
            $offset,
133
            $limit,
134
            $total,
135
        ];
136
    }
137
}
138