Completed
Pull Request — master (#1324)
by Vincent
03:47
created

OrderExtension::applyToCollection()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 4.909
c 0
b 0
f 0
cc 9
eloc 21
nc 13
nop 4
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\Orm\Extension;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18
use Doctrine\ORM\QueryBuilder;
19
20
/**
21
 * Applies selected ordering while querying resource collection.
22
 *
23
 * @author Kévin Dunglas <[email protected]>
24
 * @author Samuel ROZE <[email protected]>
25
 * @author Vincent Chalamon <[email protected]>
26
 */
27
final class OrderExtension implements QueryCollectionExtensionInterface
28
{
29
    private $order;
30
    private $resourceMetadataFactory;
31
32
    public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null)
33
    {
34
        $this->resourceMetadataFactory = $resourceMetadataFactory;
35
        $this->order = $order;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
42
    {
43
        $classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
44
        $identifiers = $classMetaData->getIdentifier();
45
        if (null !== $this->resourceMetadataFactory) {
46
            $defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
47
            if (null !== $defaultOrder) {
48
                foreach ($defaultOrder as $field => $order) {
49
                    if (is_int($field)) {
50
                        // Default direction
51
                        $field = $order;
52
                        $order = 'ASC';
53
                    }
54
                    if (false === strstr($field, '.')) {
55
                        // Configure default filter with property
56
                        $field = 'o.'.$field;
57
                    } else {
58
                        // Configure default filter with association
59
                        // Ensure query join is done on association
60
                        $associationName = substr($field, 0, strpos($field, '.'));
61
                        if (!in_array($associationName, $queryBuilder->getRootAliases(), true)) {
62
                            $queryBuilder->join('o.'.$associationName, $associationName);
63
                        }
64
                    }
65
                    $queryBuilder->addOrderBy($field, $order);
66
                }
67
68
                return;
69
            }
70
        }
71
72
        if (null !== $this->order) {
73
            foreach ($identifiers as $identifier) {
74
                $queryBuilder->addOrderBy('o.'.$identifier, $this->order);
75
            }
76
        }
77
    }
78
}
79