• 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

97.06
/src/Doctrine/Odm/Extension/OrderExtension.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\Common\PropertyHelperTrait;
17
use ApiPlatform\Doctrine\Odm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
18
use ApiPlatform\Metadata\Operation;
19
use Doctrine\ODM\MongoDB\Aggregation\Builder;
20
use Doctrine\ODM\MongoDB\Aggregation\Stage\Sort;
21
use Doctrine\Persistence\ManagerRegistry;
22

23
/**
24
 * Applies selected ordering while querying resource collection.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 * @author Samuel ROZE <samuel.roze@gmail.com>
28
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
29
 * @author Alan Poulain <contact@alanpoulain.eu>
30
 */
31
final class OrderExtension implements AggregationCollectionExtensionInterface
32
{
33
    use MongoDbOdmPropertyHelperTrait;
34
    use PropertyHelperTrait;
35

36
    public function __construct(private readonly ?string $order = null, private readonly ?ManagerRegistry $managerRegistry = null)
37
    {
UNCOV
38
    }
274✔
39

40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass, ?Operation $operation = null, array &$context = []): void
44
    {
45
        // Do not apply order if already defined on $aggregationBuilder
UNCOV
46
        if ($this->hasSortStage($aggregationBuilder)) {
274✔
UNCOV
47
            return;
17✔
48
        }
49

UNCOV
50
        $classMetaData = $this->getClassMetadata($resourceClass);
257✔
UNCOV
51
        $identifiers = $classMetaData->getIdentifier();
257✔
UNCOV
52
        if (isset($context['operation'])) {
257✔
UNCOV
53
            $defaultOrder = $context['operation']->getOrder() ?? [];
257✔
54
        } else {
55
            $defaultOrder = $operation?->getOrder();
×
56
        }
57

UNCOV
58
        if ($defaultOrder) {
257✔
UNCOV
59
            foreach ($defaultOrder as $field => $order) {
9✔
UNCOV
60
                if (\is_int($field)) {
9✔
61
                    // Default direction
UNCOV
62
                    $field = $order;
7✔
UNCOV
63
                    $order = 'ASC';
7✔
64
                }
65

UNCOV
66
                if ($this->isPropertyNested($field, $resourceClass)) {
9✔
UNCOV
67
                    [$field] = $this->addLookupsForNestedProperty($field, $aggregationBuilder, $resourceClass, true);
5✔
68
                }
UNCOV
69
                $aggregationBuilder->sort(
9✔
UNCOV
70
                    $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$field => $order]
9✔
UNCOV
71
                );
9✔
72
            }
73

UNCOV
74
            return;
9✔
75
        }
76

UNCOV
77
        if (null !== $this->order) {
250✔
UNCOV
78
            foreach ($identifiers as $identifier) {
250✔
UNCOV
79
                $aggregationBuilder->sort(
250✔
UNCOV
80
                    $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$identifier => $this->order]
250✔
UNCOV
81
                );
250✔
82
            }
83
        }
84
    }
85

86
    protected function getManagerRegistry(): ManagerRegistry
87
    {
UNCOV
88
        return $this->managerRegistry;
257✔
89
    }
90

91
    private function hasSortStage(Builder $aggregationBuilder): bool
92
    {
UNCOV
93
        $shouldStop = false;
274✔
UNCOV
94
        $index = 0;
274✔
95

96
        do {
97
            try {
UNCOV
98
                if ($aggregationBuilder->getStage($index) instanceof Sort) {
274✔
99
                    // If at least one stage is sort, then it has sorting
UNCOV
100
                    return true;
138✔
101
                }
UNCOV
102
            } catch (\OutOfRangeException $outOfRangeException) {
257✔
103
                // There is no more stages on the aggregation builder
UNCOV
104
                $shouldStop = true;
257✔
105
            }
106

UNCOV
107
            ++$index;
260✔
UNCOV
108
        } while (!$shouldStop);
260✔
109

110
        // No stage was sort, and we iterated through all stages
UNCOV
111
        return false;
257✔
112
    }
113
}
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