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
|
|
|
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension; |
13
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
15
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use Doctrine\ODM\MongoDB\Query\Builder; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
20
|
|
|
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Paginator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Applies pagination on the Doctrine query for resource collection when enabled. |
24
|
|
|
* |
25
|
|
|
* @author Kévin Dunglas <[email protected]> |
26
|
|
|
* @author Samuel ROZE <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class PaginationExtension implements QueryResultExtensionInterface |
29
|
|
|
{ |
30
|
|
|
private $managerRegistry; |
31
|
|
|
private $requestStack; |
32
|
|
|
private $resourceMetadataFactory; |
33
|
|
|
private $enabled; |
34
|
|
|
private $clientEnabled; |
35
|
|
|
private $clientItemsPerPage; |
36
|
|
|
private $itemsPerPage; |
37
|
|
|
private $pageParameterName; |
38
|
|
|
private $enabledParameterName; |
39
|
|
|
private $itemsPerPageParameterName; |
40
|
|
|
|
41
|
|
View Code Duplication |
public function __construct(ManagerRegistry $managerRegistry, RequestStack $requestStack, ResourceMetadataFactoryInterface $resourceMetadataFactory, bool $enabled = true, bool $clientEnabled = false, bool $clientItemsPerPage = false, int $itemsPerPage = 30, string $pageParameterName = 'page', string $enabledParameterName = 'pagination', string $itemsPerPageParameterName = 'itemsPerPage') |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->managerRegistry = $managerRegistry; |
44
|
|
|
$this->requestStack = $requestStack; |
45
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
46
|
|
|
$this->enabled = $enabled; |
47
|
|
|
$this->clientEnabled = $clientEnabled; |
48
|
|
|
$this->clientItemsPerPage = $clientItemsPerPage; |
49
|
|
|
$this->itemsPerPage = $itemsPerPage; |
50
|
|
|
$this->pageParameterName = $pageParameterName; |
51
|
|
|
$this->enabledParameterName = $enabledParameterName; |
52
|
|
|
$this->itemsPerPageParameterName = $itemsPerPageParameterName; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function applyToCollection(Builder $queryBuilder, string $resourceClass, string $operationName = null) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
61
|
|
|
if (null === $request) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
66
|
|
|
if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true); |
71
|
|
|
if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) { |
72
|
|
|
$itemsPerPage = (int) $request->query->get($this->itemsPerPageParameterName, $itemsPerPage); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$queryBuilder |
76
|
|
|
->skip(($request->query->get($this->pageParameterName, 1) - 1) * $itemsPerPage) |
77
|
|
|
->limit($itemsPerPage) |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function supportsResult(string $resourceClass, string $operationName = null) : bool |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
87
|
|
|
if (null === $request) { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
92
|
|
|
|
93
|
|
|
return $this->isPaginationEnabled($request, $resourceMetadata, $operationName); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function getResult(Builder $queryBuilder) |
100
|
|
|
{ |
101
|
|
|
return new Paginator($queryBuilder->getQuery()->execute()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null) : bool |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true); |
107
|
|
|
$clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true); |
108
|
|
|
|
109
|
|
|
if ($clientEnabled) { |
110
|
|
|
$enabled = filter_var($request->query->get($this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $enabled; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.