Completed
Pull Request — master (#230)
by Beñat
03:20
created

TasksResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 11
loc 11
rs 9.4285
c 1
b 0
f 0
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 resolveByProject($projectId, $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
        $args['projectId'] = $projectId;
46
47
        return $this->resolve($args);
48
    }
49
50
    public function resolveByParent($parentId, $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...
51
    {
52
        $args['parentId'] = $parentId;
53
54
        return $this->resolve($args);
55
    }
56
57
    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...
58
    {
59
        $args = $args['taskConnectionInput'];
60
61
        if (!isset($args['title'])) {
62
            $args['title'] = null;
63
        }
64
        if (!isset($args['projectId'])) {
65
            $args['projectId'] = null;
66
        }
67
        if (!isset($args['parentId'])) {
68
            $args['parentId'] = null;
69
        }
70
        if (!isset($args['priority'])) {
71
            $args['priority'] = null;
72
        }
73
        if (!isset($args['progress'])) {
74
            $args['progress'] = null;
75
        }
76
        if (!isset($args['assigneeId'])) {
77
            $args['assigneeId'] = null;
78
        }
79
        if (!isset($args['creatorId'])) {
80
            $args['creatorId'] = null;
81
        }
82
83
        list($offset, $limit, $total) = $this->buildPagination($args);
84
85
        $this->queryBus->handle(
86
            new FilterTasksQuery(
87
                $this->currentUser,
88
                $offset,
89
                $limit,
90
                $args['parentId'],
91
                $args['projectId'],
92
                $args['title'],
93
                $args['priority'],
94
                $args['progress'],
95
                $args['assigneeId'],
96
                $args['creatorId']
97
            ),
98
            $result
99
        );
100
101
        foreach ($result as $key => $task) {
102
            $result[$key] = $this->taskBuilderResolver->resolve($task);
103
        }
104
105
        $connection = $this->connectionBuilder->fromArraySlice(
106
            $result,
107
            $args,
108
            [
109
                'sliceStart'  => $offset,
110
                'arrayLength' => $total,
111
            ]
112
        );
113
        $connection->totalCount = count($result);
114
115
        return $connection;
116
    }
117
118
    private function buildPagination($args) : array
119
    {
120
        $this->queryBus->handle(
121
            new CountTasksQuery(
122
                $this->currentUser,
123
                $args['parentId'],
124
                $args['projectId'],
125
                $args['title'],
126
                $args['priority'],
127
                $args['progress'],
128
                $args['assigneeId'],
129
                $args['creatorId']
130
            ),
131
            $total
132
        );
133
134
        $beforeOffset = $this->connectionBuilder->getOffsetWithDefault(
135
            isset($args['before'])
136
                ? $args['before']
137
                : null,
138
            $total
139
        );
140
        $afterOffset = $this->connectionBuilder->getOffsetWithDefault(
141
            isset($args['after'])
142
                ? $args['after']
143
                : null,
144
            -1
145
        );
146
        $startOffset = max($afterOffset, -1) + 1;
147
        $endOffset = min($beforeOffset, $total);
148
149
        if (isset($args['first']) && is_numeric($args['first'])) {
150
            $endOffset = min($endOffset, $startOffset + $args['first']);
151
        }
152
        if (isset($args['last']) && is_numeric($args['last'])) {
153
            $startOffset = max($startOffset, $endOffset - $args['last']);
154
        }
155
        $offset = max($startOffset, 0);
156
        $limit = $endOffset - $startOffset;
157
158
        return [
159
            $offset,
160
            $limit,
161
            $total,
162
        ];
163
    }
164
}
165