Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

Pagination::getGraphQlEnabled()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\DataProvider;
15
16
use ApiPlatform\Core\Exception\InvalidArgumentException;
17
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
20
/**
21
 * Pagination configuration.
22
 *
23
 * @author Baptiste Meyer <[email protected]>
24
 */
25
final class Pagination
26
{
27
    private $options;
28
    private $graphQlOptions;
29
    private $resourceMetadataFactory;
30
31
    public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, array $options = [], array $graphQlOptions = [])
32
    {
33
        $this->resourceMetadataFactory = $resourceMetadataFactory;
34
        $this->options = array_merge([
35
            'enabled' => true,
36
            'client_enabled' => false,
37
            'client_items_per_page' => false,
38
            'items_per_page' => 30,
39
            'page_default' => 1,
40
            'page_parameter_name' => 'page',
41
            'enabled_parameter_name' => 'pagination',
42
            'items_per_page_parameter_name' => 'itemsPerPage',
43
            'maximum_items_per_page' => null,
44
            'partial' => false,
45
            'client_partial' => false,
46
            'partial_parameter_name' => 'partial',
47
        ], $options);
48
        $this->graphQlOptions = array_merge([
49
            'enabled' => true,
50
        ], $graphQlOptions);
51
    }
52
53
    /**
54
     * Gets the current page.
55
     *
56
     * @throws InvalidArgumentException
57
     */
58
    public function getPage(array $context = []): int
59
    {
60
        $page = (int) $this->getParameterFromContext(
61
            $context,
62
            $this->options['page_parameter_name'],
63
            $this->options['page_default']
64
        );
65
66
        if (1 > $page) {
67
            throw new InvalidArgumentException('Page should not be less than 1');
68
        }
69
70
        return $page;
71
    }
72
73
    /**
74
     * Gets the current offset.
75
     */
76
    public function getOffset(string $resourceClass = null, string $operationName = null, array $context = []): int
77
    {
78
        $graphql = (bool) ($context['graphql_operation_name'] ?? false);
79
80
        $limit = $this->getLimit($resourceClass, $operationName, $context);
81
82
        if ($graphql && null !== ($after = $this->getParameterFromContext($context, 'after'))) {
83
            return false === ($after = base64_decode($after, true)) ? 0 : (int) $after + 1;
84
        }
85
86
        if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))) {
87
            return ($offset = (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit)) < 0 ? 0 : $offset;
88
        }
89
90
        if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
91
            return ($offset = ($context['count'] ?? 0) - $last) < 0 ? 0 : $offset;
92
        }
93
94
        return ($this->getPage($context) - 1) * $limit;
95
    }
96
97
    /**
98
     * Gets the current limit.
99
     *
100
     * @throws InvalidArgumentException
101
     */
102
    public function getLimit(string $resourceClass = null, string $operationName = null, array $context = []): int
103
    {
104
        $graphql = (bool) ($context['graphql_operation_name'] ?? false);
105
106
        $limit = $this->options['items_per_page'];
107
        $clientLimit = $this->options['client_items_per_page'];
108
109
        if (null !== $resourceClass) {
110
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
111
            $limit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $limit, true);
112
            $clientLimit = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $clientLimit, true);
113
        }
114
115
        if ($graphql && null !== ($first = $this->getParameterFromContext($context, 'first'))) {
116
            $limit = $first;
117
        }
118
119
        if ($graphql && null !== ($last = $this->getParameterFromContext($context, 'last'))) {
120
            $limit = $last;
121
        }
122
123
        if ($graphql && null !== ($before = $this->getParameterFromContext($context, 'before'))
124
            && (false === ($before = base64_decode($before, true)) ? 0 : (int) $before - $limit) < 0) {
125
            $limit = (int) $before;
126
        }
127
128
        if ($clientLimit) {
129
            $limit = (int) $this->getParameterFromContext($context, $this->options['items_per_page_parameter_name'], $limit);
130
            $maxItemsPerPage = $this->options['maximum_items_per_page'];
131
132
            if (null !== $resourceClass) {
133
                $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
134
                $maxItemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'maximum_items_per_page', $maxItemsPerPage, true);
135
            }
136
137
            if (null !== $maxItemsPerPage && $limit > $maxItemsPerPage) {
138
                $limit = $maxItemsPerPage;
139
            }
140
        }
141
142
        if (0 > $limit) {
143
            throw new InvalidArgumentException('Limit should not be less than 0');
144
        }
145
146
        return $limit;
147
    }
148
149
    /**
150
     * Gets info about the pagination.
151
     *
152
     * Returns an array with the following info as values:
153
     *   - the page {@see Pagination::getPage()}
154
     *   - the offset {@see Pagination::getOffset()}
155
     *   - the limit {@see Pagination::getLimit()}
156
     *
157
     * @throws InvalidArgumentException
158
     */
159
    public function getPagination(string $resourceClass = null, string $operationName = null, array $context = []): array
160
    {
161
        $page = $this->getPage($context);
162
        $limit = $this->getLimit($resourceClass, $operationName, $context);
163
164
        if (0 === $limit && 1 < $page) {
165
            throw new InvalidArgumentException('Page should not be greater than 1 if limit is equal to 0');
166
        }
167
168
        return [$page, $this->getOffset($resourceClass, $operationName, $context), $limit];
169
    }
170
171
    /**
172
     * Is the pagination enabled?
173
     */
174
    public function isEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool
175
    {
176
        return $this->getEnabled($context, $resourceClass, $operationName);
177
    }
178
179
    /**
180
     * Is the pagination enabled for GraphQL?
181
     */
182
    public function isGraphQlEnabled(?string $resourceClass = null, ?string $operationName = null, array $context = []): bool
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

182
    public function isGraphQlEnabled(?string $resourceClass = null, ?string $operationName = null, /** @scrutinizer ignore-unused */ array $context = []): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
    {
184
        return $this->getGraphQlEnabled($resourceClass, $operationName);
185
    }
186
187
    /**
188
     * Is the partial pagination enabled?
189
     */
190
    public function isPartialEnabled(string $resourceClass = null, string $operationName = null, array $context = []): bool
191
    {
192
        return $this->getEnabled($context, $resourceClass, $operationName, true);
193
    }
194
195
    /**
196
     * Is the classic or partial pagination enabled?
197
     */
198
    private function getEnabled(array $context, string $resourceClass = null, string $operationName = null, bool $partial = false): bool
199
    {
200
        $enabled = $this->options[$partial ? 'partial' : 'enabled'];
201
        $clientEnabled = $this->options[$partial ? 'client_partial' : 'client_enabled'];
202
203
        if (null !== $resourceClass) {
204
            $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
205
            $enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_partial' : 'pagination_enabled', $enabled, true);
206
207
            $clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, $partial ? 'pagination_client_partial' : 'pagination_client_enabled', $clientEnabled, true);
208
        }
209
210
        if ($clientEnabled) {
211
            return filter_var($this->getParameterFromContext($context, $this->options[$partial ? 'partial_parameter_name' : 'enabled_parameter_name'], $enabled), FILTER_VALIDATE_BOOLEAN);
212
        }
213
214
        return (bool) $enabled;
215
    }
216
217
    private function getGraphQlEnabled(?string $resourceClass, ?string $operationName): bool
218
    {
219
        $enabled = $this->graphQlOptions['enabled'];
220
221
        if (null !== $resourceClass) {
222
            try {
223
                $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
224
            } catch (ResourceClassNotFoundException $e) {
225
                return $enabled;
226
            }
227
228
            return (bool) $resourceMetadata->getGraphqlAttribute($operationName, 'pagination_enabled', $enabled, true);
0 ignored issues
show
Bug introduced by
It seems like $operationName can also be of type null; however, parameter $operationName of ApiPlatform\Core\Metadat...::getGraphqlAttribute() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

228
            return (bool) $resourceMetadata->getGraphqlAttribute(/** @scrutinizer ignore-type */ $operationName, 'pagination_enabled', $enabled, true);
Loading history...
229
        }
230
231
        return $enabled;
232
    }
233
234
    /**
235
     * Gets the given pagination parameter name from the given context.
236
     *
237
     * @param mixed|null $default
238
     */
239
    private function getParameterFromContext(array $context, string $parameterName, $default = null)
240
    {
241
        $filters = $context['filters'] ?? [];
242
243
        return \array_key_exists($parameterName, $filters) ? $filters[$parameterName] : $default;
244
    }
245
}
246