Passed
Pull Request — master (#2144)
by Alan
03:21
created

OrderExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
B applyToCollection() 0 34 9
A __construct() 0 5 1
A getManagerRegistry() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\Bridge\Doctrine\MongoDbOdm\Extension;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\PropertyHelperTrait;
17
use ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\PropertyHelperTrait as MongoDbOdmPropertyHelperTrait;
18
use ApiPlatform\Core\Exception\InvalidArgumentException;
19
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
20
use Doctrine\Common\Persistence\ManagerRegistry;
21
use Doctrine\ODM\MongoDB\Aggregation\Builder;
22
23
/**
24
 * Applies selected ordering while querying resource collection.
25
 *
26
 * @experimental
27
 *
28
 * @author Kévin Dunglas <[email protected]>
29
 * @author Samuel ROZE <[email protected]>
30
 * @author Vincent Chalamon <[email protected]>
31
 * @author Alan Poulain <[email protected]>
32
 */
33
final class OrderExtension implements AggregationCollectionExtensionInterface
34
{
35
    use PropertyHelperTrait;
36
    use MongoDbOdmPropertyHelperTrait;
37
38
    private $order;
39
    private $resourceMetadataFactory;
40
    private $managerRegistry;
41
42
    public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null, ManagerRegistry $managerRegistry = null)
43
    {
44
        $this->resourceMetadataFactory = $resourceMetadataFactory;
45
        $this->order = $order;
46
        $this->managerRegistry = $managerRegistry;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function applyToCollection(Builder $aggregationBuilder, string $resourceClass = null, string $operationName = null, array &$context = [])
53
    {
54
        if (null === $resourceClass) {
55
            throw new InvalidArgumentException('The "$resourceClass" parameter must not be null');
56
        }
57
58
        $classMetaData = $this->getClassMetadata($resourceClass);
59
        $identifiers = $classMetaData->getIdentifier();
60
        if (null !== $this->resourceMetadataFactory) {
61
            $defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
62
            if (null !== $defaultOrder) {
63
                foreach ($defaultOrder as $field => $order) {
64
                    if (\is_int($field)) {
65
                        // Default direction
66
                        $field = $order;
67
                        $order = 'ASC';
68
                    }
69
70
                    if ($this->isPropertyNested($field, $resourceClass)) {
71
                        [$field] = $this->addLookupsForNestedProperty($field, $aggregationBuilder, $resourceClass);
72
                    }
73
                    $aggregationBuilder->sort(
74
                        $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$field => $order]
75
                    );
76
                }
77
78
                return;
79
            }
80
        }
81
82
        if (null !== $this->order) {
83
            foreach ($identifiers as $identifier) {
84
                $aggregationBuilder->sort(
85
                    $context['mongodb_odm_sort_fields'] = ($context['mongodb_odm_sort_fields'] ?? []) + [$identifier => $this->order]
86
                );
87
            }
88
        }
89
    }
90
91
    protected function getManagerRegistry(): ManagerRegistry
92
    {
93
        return $this->managerRegistry;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->managerRegistry could return the type null which is incompatible with the type-hinted return Doctrine\Common\Persistence\ManagerRegistry. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
}
96