|
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); |
|
|
|
|
|
|
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'])) { |
|
|
|
|
|
|
90
|
|
|
$endOffset = min($endOffset, $startOffset + $args['first']); |
|
91
|
|
|
} |
|
92
|
|
View Code Duplication |
if (isset($args['last']) && is_numeric($args['last'])) { |
|
|
|
|
|
|
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
|
|
|
|
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.