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

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 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
    }
548✔
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)) {
548✔
UNCOV
47
            return;
34✔
48
        }
49

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

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

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

UNCOV
74
            return;
18✔
75
        }
76

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

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

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

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

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

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

© 2026 Coveralls, Inc