Completed
Pull Request — master (#228)
by Beñat
03:47
created

TasksResolver::resolveByParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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