Completed
Pull Request — master (#180)
by Gorka
05:29
created

OrganizationsResolver::resolve()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.8571
cc 2
eloc 17
nc 2
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\Ui\Web\Api\GraphQL\Query\Organization;
16
17
use Kreta\SharedKernel\Application\QueryBus;
18
use Kreta\TaskManager\Application\Query\Organization\CountOrganizationsQuery;
19
use Kreta\TaskManager\Application\Query\Organization\FilterOrganizationsQuery;
20
use Kreta\TaskManager\Infrastructure\Ui\Web\Api\GraphQL\Query\Resolver;
21
use Overblog\GraphQLBundle\Relay\Connection\Output\ConnectionBuilder;
22
23
class OrganizationsResolver implements Resolver
24
{
25
    private $queryBus;
26
27
    public function __construct(QueryBus $queryBus)
28
    {
29
        $this->queryBus = $queryBus;
30
    }
31
32
    public function resolve($args)
33
    {
34
        if (!isset($args['name'])) {
35
            $args['name'] = null;
36
        }
37
38
        list($offset, $limit, $total) = $this->buildPagination($args);
39
40
        $this->queryBus->handle(
41
            new FilterOrganizationsQuery(
42
                $offset,
43
                $limit,
44
                $args['name']
45
            ),
46
            $result
47
        );
48
49
        $connection = ConnectionBuilder::connectionFromArraySlice(
50
            $result,
51
            $args,
52
            [
53
                'sliceStart'  => $offset,
54
                'arrayLength' => $total,
55
            ]
56
        );
57
        $connection->totalCount = count($result);
0 ignored issues
show
Bug introduced by
The property totalCount does not seem to exist in Overblog\GraphQLBundle\R...ction\Output\Connection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
59
        return $connection;
60
    }
61
62
    private function buildPagination($args)
63
    {
64
        $this->queryBus->handle(
65
            new CountOrganizationsQuery(
66
                $args['name']
67
            ),
68
            $total
69
        );
70
71
        // AL COUNT QUERY ESTE DE ARRIBA HAY QUE PASARLE EL $name y en vez de usar el size del repo hay que hacer
72
        // como hacemos en el CMS con el buildQuery y el buildCount
73
74
        $beforeOffset = ConnectionBuilder::getOffsetWithDefault(
75
            isset($args['before'])
76
                ? $args['before']
77
                : null,
78
            $total
79
        );
80
        $afterOffset = ConnectionBuilder::getOffsetWithDefault(
81
            isset($args['after'])
82
                ? $args['after']
83
                : null,
84
            -1
85
        );
86
        $startOffset = max($afterOffset, -1) + 1;
87
        $endOffset = min($beforeOffset, $total);
88
89 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...
90
            $endOffset = min($endOffset, $startOffset + $args['first']);
91
        }
92 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...
93
            $startOffset = max($startOffset, $endOffset - $args['last']);
94
        }
95
        $offset = max($startOffset, 0);
96
        $limit = $endOffset - $startOffset;
97
98
        return [
99
            $offset,
100
            $limit,
101
            $total,
102
        ];
103
    }
104
}
105