Passed
Pull Request — master (#2144)
by Alan
03:26
created

PaginationExtension::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 4
dl 0
loc 3
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\Bridge\Doctrine\MongoDbOdm\Extension;
15
16
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Paginator;
17
use ApiPlatform\Core\DataProvider\Pagination;
18
use Doctrine\Common\Persistence\ManagerRegistry;
19
use Doctrine\ODM\MongoDB\Aggregation\Builder;
20
21
/**
22
 * Applies pagination on the Doctrine aggregation for resource collection when enabled.
23
 *
24
 * @experimental
25
 *
26
 * @author Kévin Dunglas <[email protected]>
27
 * @author Samuel ROZE <[email protected]>
28
 * @author Alan Poulain <[email protected]>
29
 */
30
final class PaginationExtension implements AggregationResultCollectionExtensionInterface
31
{
32
    private $managerRegistry;
33
    private $pagination;
34
35
    public function __construct(ManagerRegistry $managerRegistry, Pagination $pagination)
36
    {
37
        $this->managerRegistry = $managerRegistry;
38
        $this->pagination = $pagination;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = [])
45
    {
46
        if (!$this->pagination->isEnabled($resourceClass, $operationName)) {
47
            return;
48
        }
49
50
        [, $offset, $limit] = $this->pagination->getPagination($resourceClass, $operationName);
51
52
        $repository = $this->managerRegistry->getManagerForClass($resourceClass)->getRepository($resourceClass);
53
        $aggregationBuilder
54
            ->facet()
55
            ->field('results')->pipeline(
56
                $repository->createAggregationBuilder()
0 ignored issues
show
Bug introduced by
The method createAggregationBuilder() does not exist on Doctrine\Common\Persistence\ObjectRepository. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\ODM\MongoDB\Repository\GridFSRepository. Are you sure you never get one of those? ( Ignorable by Annotation )

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

56
                $repository->/** @scrutinizer ignore-call */ 
57
                             createAggregationBuilder()
Loading history...
57
                    ->skip($offset)
58
                    ->limit($limit)
59
            )
60
            ->field('count')->pipeline(
61
                $repository->createAggregationBuilder()
62
                    ->count('count')
63
            );
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function supportsResult(string $resourceClass, string $operationName = null, array $context = []): bool
70
    {
71
        return $this->pagination->isEnabled($resourceClass, $operationName);
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function getResult(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array $context = [])
78
    {
79
        return new Paginator($aggregationBuilder->execute(), $this->managerRegistry->getManagerForClass($resourceClass)->getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline());
0 ignored issues
show
Bug introduced by
The method getUnitOfWork() does not exist on Doctrine\Common\Persistence\ObjectManager. It seems like you code against a sub-type of said class. However, the method does not exist in Doctrine\Common\Persistence\ObjectManagerDecorator. Are you sure you never get one of those? ( Ignorable by Annotation )

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

79
        return new Paginator($aggregationBuilder->execute(), $this->managerRegistry->getManagerForClass($resourceClass)->/** @scrutinizer ignore-call */ getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline());
Loading history...
80
    }
81
}
82