Completed
Pull Request — master (#226)
by Beñat
03:39
created

TasksResolver::resolve()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 57
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 37
nc 64
nop 1
dl 0
loc 57
rs 7.6759
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Task;
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\ProjectOfIdQuery;
21
use Kreta\TaskManager\Application\Query\Project\Task\CountTasksQuery;
22
use Kreta\TaskManager\Application\Query\Project\Task\FilterTasksQuery;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
25
class TasksResolver implements Resolver
26
{
27
    private $connectionBuilder;
28
    private $queryBus;
29
    private $currentUser;
30
31 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...
32
        TokenStorageInterface $tokenStorage,
33
        ConnectionBuilder $connectionBuilder,
34
        QueryBus $queryBus
35
    ) {
36
        $this->connectionBuilder = $connectionBuilder;
37
        $this->queryBus = $queryBus;
38
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
39
    }
40
41
    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...
42
    {
43
        if (!isset($args['title'])) {
44
            $args['title'] = null;
45
        }
46
        if (!isset($args['projectId'])) {
47
            $args['projectId'] = null;
48
        }
49
        if (!isset($args['parentId'])) {
50
            $args['parentId'] = null;
51
        }
52
        if (!isset($args['priority'])) {
53
            $args['priority'] = null;
54
        }
55
        if (!isset($args['progress'])) {
56
            $args['progress'] = null;
57
        }
58
59
        list($offset, $limit, $total) = $this->buildPagination($args);
60
61
        $this->queryBus->handle(
62
            new FilterTasksQuery(
63
                $this->currentUser,
64
                $offset,
65
                $limit,
66
                $args['parentId'],
67
                $args['projectId'],
68
                $args['title'],
69
                $args['priority'],
70
                $args['progress']
71
            ),
72
            $result
73
        );
74
75
        foreach ($result as $key => $task) {
76
            $this->queryBus->handle(
77
                new ProjectOfIdQuery(
78
                    $task['project_id'],
79
                    $this->currentUser
80
                ),
81
                $result[$key]['project']
82
            );
83
            unset($result[$key]['project_id']);
84
        }
85
86
        $connection = $this->connectionBuilder->fromArraySlice(
87
            $result,
88
            $args,
89
            [
90
                'sliceStart'  => $offset,
91
                'arrayLength' => $total,
92
            ]
93
        );
94
        $connection->totalCount = count($result);
95
96
        return $connection;
97
    }
98
99
    private function buildPagination($args)
100
    {
101
        $this->queryBus->handle(
102
            new CountTasksQuery(
103
                $this->currentUser,
104
                $args['parentId'],
105
                $args['projectId'],
106
                $args['title'],
107
                $args['priority'],
108
                $args['progress']
109
            ),
110
            $total
111
        );
112
113
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
114
            isset($args['before'])
115
                ? $args['before']
116
                : null,
117
            $total
118
        );
119
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
120
            isset($args['after'])
121
                ? $args['after']
122
                : null,
123
            -1
124
        );
125
        $startOffset = max($afterOffset, -1) + 1;
126
        $endOffset = min($beforeOffset, $total);
127
128
        if (isset($args['first']) && is_numeric($args['first'])) {
129
            $endOffset = min($endOffset, $startOffset + $args['first']);
130
        }
131
        if (isset($args['last']) && is_numeric($args['last'])) {
132
            $startOffset = max($startOffset, $endOffset - $args['last']);
133
        }
134
        $offset = max($startOffset, 0);
135
        $limit = $endOffset - $startOffset;
136
137
        return [
138
            $offset,
139
            $limit,
140
            $total,
141
        ];
142
    }
143
}
144