Completed
Pull Request — master (#212)
by Beñat
06:38 queued 02:56
created

ProjectsResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 53.76 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 50
loc 93
rs 10
wmc 11
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
B resolve() 0 34 3
C buildPagination() 41 41 7

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\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 Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
24
class ProjectsResolver implements Resolver
25
{
26
    private $connectionBuilder;
27
    private $queryBus;
28
    private $currentUser;
29
30 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...
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['organizationId'])) {
46
            $args['organizationId'] = null;
47
        }
48
49
        list($offset, $limit, $total) = $this->buildPagination($args);
50
51
        $this->queryBus->handle(
52
            new FilterProjectsQuery(
53
                $this->currentUser,
54
                $offset,
55
                $limit,
56
                $args['organizationId'],
57
                $args['name']
58
            ),
59
            $result
60
        );
61
62
        $connection = $this->connectionBuilder->fromArraySlice(
63
            $result,
64
            $args,
65
            [
66
                'sliceStart'  => $offset,
67
                'arrayLength' => $total,
68
            ]
69
        );
70
        $connection->totalCount = count($result);
71
72
        return $connection;
73
    }
74
75 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...
76
    {
77
        $this->queryBus->handle(
78
            new CountProjectsQuery(
79
                $this->currentUser,
80
                $args['organizationId'],
81
                $args['name']
82
            ),
83
            $total
84
        );
85
86
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
87
            isset($args['before'])
88
                ? $args['before']
89
                : null,
90
            $total
91
        );
92
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
93
            isset($args['after'])
94
                ? $args['after']
95
                : null,
96
            -1
97
        );
98
        $startOffset = max($afterOffset, -1) + 1;
99
        $endOffset = min($beforeOffset, $total);
100
101
        if (isset($args['first']) && is_numeric($args['first'])) {
102
            $endOffset = min($endOffset, $startOffset + $args['first']);
103
        }
104
        if (isset($args['last']) && is_numeric($args['last'])) {
105
            $startOffset = max($startOffset, $endOffset - $args['last']);
106
        }
107
        $offset = max($startOffset, 0);
108
        $limit = $endOffset - $startOffset;
109
110
        return [
111
            $offset,
112
            $limit,
113
            $total,
114
        ];
115
    }
116
}
117