Passed
Pull Request — master (#2144)
by Alan
04:06
created

PaginationExtension::isPaginationEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 3
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
 * This file is part of the API Platform project.
15
 *
16
 * (c) Kévin Dunglas <[email protected]>
17
 *
18
 * For the full copyright and license information, please view the LICENSE
19
 * file that was distributed with this source code.
20
 */
21
22
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension;
23
24
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Paginator;
25
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
26
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
27
use Doctrine\Common\Persistence\ManagerRegistry;
28
use Doctrine\ODM\MongoDB\Query\Builder;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\RequestStack;
31
32
/**
33
 * Applies pagination on the Doctrine query for resource collection when enabled.
34
 *
35
 * @author Kévin Dunglas <[email protected]>
36
 * @author Samuel ROZE <[email protected]>
37
 */
38
class PaginationExtension implements QueryResultExtensionInterface
39
{
40
    private $managerRegistry;
41
    private $requestStack;
42
    private $resourceMetadataFactory;
43
    private $enabled;
44
    private $clientEnabled;
45
    private $clientItemsPerPage;
46
    private $itemsPerPage;
47
    private $pageParameterName;
48
    private $enabledParameterName;
49
    private $itemsPerPageParameterName;
50
51
    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')
52
    {
53
        $this->managerRegistry = $managerRegistry;
54
        $this->requestStack = $requestStack;
55
        $this->resourceMetadataFactory = $resourceMetadataFactory;
56
        $this->enabled = $enabled;
57
        $this->clientEnabled = $clientEnabled;
58
        $this->clientItemsPerPage = $clientItemsPerPage;
59
        $this->itemsPerPage = $itemsPerPage;
60
        $this->pageParameterName = $pageParameterName;
61
        $this->enabledParameterName = $enabledParameterName;
62
        $this->itemsPerPageParameterName = $itemsPerPageParameterName;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function applyToCollection(Builder $queryBuilder, string $resourceClass, string $operationName = null)
69
    {
70
        $request = $this->requestStack->getCurrentRequest();
71
        if (null === $request) {
72
            return;
73
        }
74
75
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
76
        if (!$this->isPaginationEnabled($request, $resourceMetadata, $operationName)) {
77
            return;
78
        }
79
80
        $itemsPerPage = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_items_per_page', $this->itemsPerPage, true);
81
        if ($resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_items_per_page', $this->clientItemsPerPage, true)) {
82
            $itemsPerPage = (int) $request->query->get($this->itemsPerPageParameterName, $itemsPerPage);
83
        }
84
85
        $queryBuilder
86
            ->skip(($request->query->get($this->pageParameterName, 1) - 1) * $itemsPerPage)
87
            ->limit($itemsPerPage);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function supportsResult(string $resourceClass, string $operationName = null): bool
94
    {
95
        $request = $this->requestStack->getCurrentRequest();
96
        if (null === $request) {
97
            return false;
98
        }
99
100
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
101
102
        return $this->isPaginationEnabled($request, $resourceMetadata, $operationName);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getResult(Builder $queryBuilder)
109
    {
110
        return new Paginator($queryBuilder->getQuery()->execute());
111
    }
112
113
    private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null): bool
114
    {
115
        $enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true);
116
        $clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true);
117
118
        if ($clientEnabled) {
119
            $enabled = filter_var($request->query->get($this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN);
120
        }
121
122
        return $enabled;
123
    }
124
}
125