Completed
Push — refactoring-session ( a6dec9 )
by Kamil
06:43
created

TranslationOrderNameAndLocaleFilter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 64
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterProperty() 0 38 4
A getDescription() 0 22 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ApiBundle\Filters;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\OrderFilterInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
18
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
19
use Doctrine\ORM\QueryBuilder;
20
21
final class TranslationOrderNameAndLocaleFilter extends AbstractContextAwareFilter
22
{
23
    protected function filterProperty(
24
        string $property,
25
        $value,
26
        QueryBuilder $queryBuilder,
27
        QueryNameGeneratorInterface $queryNameGenerator,
28
        string $resourceClass,
29
        string $operationName = null
30
    ): void {
31
        if ('order' === $property) {
32
            if (!isset($value['translation.name'])) {
33
                return;
34
            }
35
36
            $direction = $value['translation.name'];
37
38
            if (isset($value['localeCode'])) {
39
                $queryBuilder
40
                    ->addSelect('translation')
41
                    ->innerJoin(
42
                        sprintf('%s.translations', $queryBuilder->getRootAliases()[0]),
43
                        'translation',
44
                        'WITH',
45
                        'translation.locale = :locale'
46
                    )
47
                    ->orderBy('translation.name', $direction)
48
                    ->setParameter('locale', $value['localeCode'])
49
                ;
50
51
                return;
52
            }
53
54
            $queryBuilder
55
                ->addSelect('translation')
56
                ->innerJoin('o.translations', 'translation')
57
                ->orderBy('translation.name', $direction)
58
            ;
59
        }
60
    }
61
62
    public function getDescription(string $resourceClass): array
63
    {
64
        return [
65
            'order[translation.name]' => [
66
                'type' => 'string',
67
                'required' => false,
68
                'property' => 'translation',
69
                'schema' => [
70
                    'type' => 'string',
71
                    'enum' => [
72
                        strtolower(OrderFilterInterface::DIRECTION_ASC),
73
                        strtolower(OrderFilterInterface::DIRECTION_DESC),
74
                    ],
75
                ],
76
            ],
77
            'localeCode for order[translation.name]' => [
78
                'type' => 'string',
79
                'required' => false,
80
                'property' => 'localeCode',
81
            ],
82
        ];
83
    }
84
}
85