Completed
Pull Request — master (#227)
by Beñat
03:56
created

TasksResolver::resolve()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 58
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

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