Completed
Pull Request — master (#187)
by Beñat
03:22
created

OrganizationsResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 7.41 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 6
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B resolve() 0 29 2
C buildPagination() 6 39 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\Organization;
16
17
use Kreta\SharedKernel\Application\QueryBus;
18
use Kreta\SharedKernel\Http\GraphQl\Resolver;
19
use Kreta\TaskManager\Application\Query\Organization\CountOrganizationsQuery;
20
use Kreta\TaskManager\Application\Query\Organization\FilterOrganizationsQuery;
21
use Kreta\SharedKernel\Http\GraphQl\Relay\ConnectionBuilder;
22
23
class OrganizationsResolver implements Resolver
24
{
25
    private $connectionBuilder;
26
    private $queryBus;
27
28
    public function __construct(ConnectionBuilder $connectionBuilder, QueryBus $queryBus)
29
    {
30
        $this->connectionBuilder = $connectionBuilder;
31
        $this->queryBus = $queryBus;
32
    }
33
34
    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...
35
    {
36
        if (!isset($args['name'])) {
37
            $args['name'] = null;
38
        }
39
40
        list($offset, $limit, $total) = $this->buildPagination($args);
41
42
        $this->queryBus->handle(
43
            new FilterOrganizationsQuery(
44
                $offset,
45
                $limit,
46
                $args['name']
47
            ),
48
            $result
49
        );
50
51
        $connection = $this->connectionBuilder->fromArraySlice(
52
            $result,
53
            $args,
54
            [
55
                'sliceStart'  => $offset,
56
                'arrayLength' => $total,
57
            ]
58
        );
59
        $connection->totalCount = count($result);
60
61
        return $connection;
62
    }
63
64
    private function buildPagination($args)
65
    {
66
        $this->queryBus->handle(
67
            new CountOrganizationsQuery(
68
                $args['name']
69
            ),
70
            $total
71
        );
72
73
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
74
            isset($args['before'])
75
                ? $args['before']
76
                : null,
77
            $total
78
        );
79
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
80
            isset($args['after'])
81
                ? $args['after']
82
                : null,
83
            -1
84
        );
85
        $startOffset = max($afterOffset, -1) + 1;
86
        $endOffset = min($beforeOffset, $total);
87
88 View Code Duplication
        if (isset($args['first']) && is_numeric($args['first'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89
            $endOffset = min($endOffset, $startOffset + $args['first']);
90
        }
91 View Code Duplication
        if (isset($args['last']) && is_numeric($args['last'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
            $startOffset = max($startOffset, $endOffset - $args['last']);
93
        }
94
        $offset = max($startOffset, 0);
95
        $limit = $endOffset - $startOffset;
96
97
        return [
98
            $offset,
99
            $limit,
100
            $total,
101
        ];
102
    }
103
}
104