Completed
Pull Request — master (#461)
by Sam
03:51
created

PaginationExtension::applyToCollection()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 4
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
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDB\Extension;
13
14
use ApiPlatform\Core\Bridge\Doctrine\MongoDB\Paginator;
15
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
16
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Doctrine\ODM\MongoDB\Query\Builder;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\RequestStack;
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')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
     * {@inheritdoc}
82
     */
83 View Code Duplication
    public function supportsResult(string $resourceClass, string $operationName = null) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
84
    {
85
        $request = $this->requestStack->getCurrentRequest();
86
        if (null === $request) {
87
            return false;
88
        }
89
90
        $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
91
92
        return $this->isPaginationEnabled($request, $resourceMetadata, $operationName);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getResult(Builder $queryBuilder)
99
    {
100
        return new Paginator($queryBuilder->getQuery()->execute());
101
    }
102
103 View Code Duplication
    private function isPaginationEnabled(Request $request, ResourceMetadata $resourceMetadata, string $operationName = null) : bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $enabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_enabled', $this->enabled, true);
106
        $clientEnabled = $resourceMetadata->getCollectionOperationAttribute($operationName, 'pagination_client_enabled', $this->clientEnabled, true);
107
108
        if ($clientEnabled) {
109
            $enabled = filter_var($request->query->get($this->enabledParameterName, $enabled), FILTER_VALIDATE_BOOLEAN);
110
        }
111
112
        return $enabled;
113
    }
114
}
115