Completed
Pull Request — 2.1 (#1672)
by
unknown
06:30
created

OrderExtension::applyToCollection()   D

Complexity

Conditions 10
Paths 11

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 21
nc 11
nop 4

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\QueryBuilderHelper;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
19
use Doctrine\ORM\QueryBuilder;
20
21
/**
22
 * Applies selected ordering while querying resource collection.
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 * @author Samuel ROZE <[email protected]>
26
 * @author Vincent Chalamon <[email protected]>
27
 */
28
final class OrderExtension implements QueryCollectionExtensionInterface
29
{
30
    private $order;
31
    private $resourceMetadataFactory;
32
33
    public function __construct(string $order = null, ResourceMetadataFactoryInterface $resourceMetadataFactory = null)
34
    {
35
        $this->resourceMetadataFactory = $resourceMetadataFactory;
36
        $this->order = $order;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
43
    {
44
        $classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
45
        $identifiers = $classMetaData->getIdentifier();
46
        if (null !== $this->resourceMetadataFactory) {
47
            $defaultOrder = $this->resourceMetadataFactory->create($resourceClass)->getAttribute('order');
48
            if (null !== $defaultOrder) {
49
                foreach ($defaultOrder as $field => $order) {
50
                    if (\is_int($field)) {
51
                        // Default direction
52
                        $field = $order;
53
                        $order = 'ASC';
54
                    }
55
56
                    if (false === ($pos = \strpos($field, '.'))
57
                        || $pos && isset($classMetaData->embeddedClasses[\substr($field, 0, $pos)])) {
58
                        // Configure default filter with property
59
                        $field = 'o.'.$field;
60
                    } else {
61
                        $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, 'o', substr($field, 0, $pos));
62
                        $field = sprintf('%s.%s', $alias, substr($field, $pos + 1));
63
                    }
64
                    $queryBuilder->addOrderBy($field, $order);
65
                }
66
67
                return;
68
            }
69
        }
70
71
        if (null !== $this->order) {
72
            foreach ($identifiers as $identifier) {
73
                $queryBuilder->addOrderBy('o.'.$identifier, $this->order);
74
            }
75
        }
76
    }
77
}
78