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

api-platform / core / 10814494863

11 Sep 2024 03:11PM UTC coverage: 7.008% (-0.7%) from 7.679%
10814494863

push

github

web-flow
fix(state): remove resource_class change (#6607)

11516 of 164321 relevant lines covered (7.01%)

22.85 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
    {
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
46
        if ($this->hasSortStage($aggregationBuilder)) {
548✔
47
            return;
34✔
48
        }
49

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

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

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

74
            return;
18✔
75
        }
76

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

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

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

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

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

110
        // No stage was sort, and we iterated through all stages
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