• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.11
/src/Doctrine/Odm/Extension/PaginationExtension.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Odm\Extension;
15

16
use ApiPlatform\Doctrine\Odm\Paginator;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\State\Pagination\Pagination;
20
use Doctrine\ODM\MongoDB\Aggregation\Builder;
21
use Doctrine\ODM\MongoDB\DocumentManager;
22
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
23
use Doctrine\Persistence\ManagerRegistry;
24

25
/**
26
 * Applies pagination on the Doctrine aggregation for resource collection when enabled.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 * @author Samuel ROZE <samuel.roze@gmail.com>
30
 * @author Alan Poulain <contact@alanpoulain.eu>
31
 */
32
final class PaginationExtension implements AggregationResultCollectionExtensionInterface
33
{
34
    public function __construct(private readonly ManagerRegistry $managerRegistry, private readonly Pagination $pagination)
35
    {
36
    }
548✔
37

38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @throws RuntimeException
42
     */
43
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
44
    {
45
        if (!$this->pagination->isEnabled($operation, $context)) {
548✔
46
            return;
6✔
47
        }
48

49
        if (($context['graphql_operation_name'] ?? false) && !$this->pagination->isGraphQlEnabled($operation, $context)) {
542✔
50
            return;
×
51
        }
52

53
        $context = $this->addCountToContext(clone $aggregationBuilder, $context);
542✔
54

55
        [, $offset, $limit] = $this->pagination->getPagination($operation, $context);
542✔
56

57
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
538✔
58
        if (!$manager instanceof DocumentManager) {
538✔
59
            throw new RuntimeException(\sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class));
×
60
        }
61

62
        /**
63
         * @var DocumentRepository
64
         */
65
        $repository = $manager->getRepository($resourceClass);
538✔
66
        $resultsAggregationBuilder = $repository->createAggregationBuilder()->skip($offset);
538✔
67
        if ($limit > 0) {
538✔
68
            $resultsAggregationBuilder->limit($limit);
536✔
69
        } else {
70
            // Results have to be 0 but MongoDB does not support a limit equal to 0.
71
            $resultsAggregationBuilder->match()->field(Paginator::LIMIT_ZERO_MARKER_FIELD)->equals(Paginator::LIMIT_ZERO_MARKER);
4✔
72
        }
73

74
        $aggregationBuilder
538✔
75
            ->facet()
538✔
76
            ->field('results')->pipeline(
538✔
77
                $resultsAggregationBuilder
538✔
78
            )
538✔
79
            ->field('count')->pipeline(
538✔
80
                $repository->createAggregationBuilder()
538✔
81
                    ->count('count')
538✔
82
            );
538✔
83
    }
84

85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function supportsResult(string $resourceClass, ?Operation $operation = null, array $context = []): bool
89
    {
90
        if ($context['graphql_operation_name'] ?? false) {
544✔
91
            return $this->pagination->isGraphQlEnabled($operation, $context);
96✔
92
        }
93

94
        return $this->pagination->isEnabled($operation, $context);
450✔
95
    }
96

97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @throws RuntimeException
101
     */
102
    public function getResult(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array $context = []): iterable
103
    {
104
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
538✔
105
        if (!$manager instanceof DocumentManager) {
538✔
106
            throw new RuntimeException(\sprintf('The manager for "%s" must be an instance of "%s".', $resourceClass, DocumentManager::class));
×
107
        }
108

109
        $attribute = $operation?->getExtraProperties()['doctrine_mongodb'] ?? [];
538✔
110
        $executeOptions = $attribute['execute_options'] ?? [];
538✔
111

112
        return new Paginator($aggregationBuilder->execute($executeOptions), $manager->getUnitOfWork(), $resourceClass, $aggregationBuilder->getPipeline());
538✔
113
    }
114

115
    private function addCountToContext(Builder $aggregationBuilder, array $context): array
116
    {
117
        if (!($context['graphql_operation_name'] ?? false)) {
542✔
118
            return $context;
450✔
119
        }
120

121
        if (isset($context['filters']['last']) && !isset($context['filters']['before'])) {
94✔
122
            $context['count'] = $aggregationBuilder->count('count')->execute()->toArray()[0]['count'];
2✔
123
        }
124

125
        return $context;
94✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc