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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

0.0
/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
    }
×
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)) {
×
46
            return;
×
47
        }
48

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

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

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

57
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
×
58
        if (!$manager instanceof DocumentManager) {
×
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);
×
66
        $resultsAggregationBuilder = $repository->createAggregationBuilder()->skip($offset);
×
67
        if ($limit > 0) {
×
68
            $resultsAggregationBuilder->limit($limit);
×
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);
×
72
        }
73

74
        $aggregationBuilder
×
75
            ->facet()
×
76
            ->field('results')->pipeline(
×
77
                $resultsAggregationBuilder
×
78
            )
×
79
            ->field('count')->pipeline(
×
80
                $repository->createAggregationBuilder()
×
81
                    ->count('count')
×
82
            );
×
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) {
×
91
            return $this->pagination->isGraphQlEnabled($operation, $context);
×
92
        }
93

94
        return $this->pagination->isEnabled($operation, $context);
×
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);
×
105
        if (!$manager instanceof DocumentManager) {
×
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'] ?? [];
×
110
        $executeOptions = $attribute['execute_options'] ?? [];
×
111

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

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

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

125
        return $context;
×
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

© 2025 Coveralls, Inc