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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

90.91
/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
    {
UNCOV
36
    }
274✔
37

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

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

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

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

UNCOV
57
        $manager = $this->managerRegistry->getManagerForClass($resourceClass);
269✔
UNCOV
58
        if (!$manager instanceof DocumentManager) {
269✔
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
         */
UNCOV
65
        $repository = $manager->getRepository($resourceClass);
269✔
66

UNCOV
67
        $facet = $aggregationBuilder->facet();
269✔
UNCOV
68
        $addFields = $aggregationBuilder->addFields();
269✔
69

70
        // Get the results slice, from $offset to $offset + $limit
71
        // MongoDB does not support $limit: O, so we return an empty array directly
UNCOV
72
        if ($limit > 0) {
269✔
UNCOV
73
            $facet->field('results')->pipeline($repository->createAggregationBuilder()->skip($offset)->limit($limit));
268✔
74
        } else {
UNCOV
75
            $addFields->field('results')->literal([]);
2✔
76
        }
77

78
        // Count the total number of items
UNCOV
79
        $facet->field('count')->pipeline($repository->createAggregationBuilder()->count('count'));
269✔
80

81
        // Store pagination metadata, read by the Paginator
82
        // Using __ to avoid field names mapping
UNCOV
83
        $addFields->field('__api_first_result__')->literal($offset);
269✔
UNCOV
84
        $addFields->field('__api_max_results__')->literal($limit);
269✔
85
    }
86

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

UNCOV
96
        return $this->pagination->isEnabled($operation, $context);
225✔
97
    }
98

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

UNCOV
111
        $attribute = $operation?->getExtraProperties()['doctrine_mongodb'] ?? [];
269✔
UNCOV
112
        $executeOptions = $attribute['execute_options'] ?? [];
269✔
113

UNCOV
114
        return new Paginator($aggregationBuilder->getAggregation($executeOptions)->getIterator(), $manager->getUnitOfWork(), $resourceClass);
269✔
115
    }
116

117
    private function addCountToContext(Builder $aggregationBuilder, array $context): array
118
    {
UNCOV
119
        if (!($context['graphql_operation_name'] ?? false)) {
271✔
UNCOV
120
            return $context;
225✔
121
        }
122

UNCOV
123
        if (isset($context['filters']['last']) && !isset($context['filters']['before'])) {
47✔
UNCOV
124
            $context['count'] = $aggregationBuilder->count('count')->execute()->toArray()[0]['count'];
1✔
125
        }
126

UNCOV
127
        return $context;
47✔
128
    }
129
}
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