TwoLevelRelationHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterParameter() 0 15 1
A relationJoin() 0 7 1
A filterWhereClause() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bugloos\QueryFilterBundle\FilterHandler;
6
7
use Bugloos\QueryFilterBundle\FilterHandler\Contract\AbstractFilterHandler;
8
use Bugloos\QueryFilterBundle\FilterHandler\Contract\WithRelationInterface;
9
use Bugloos\QueryFilterBundle\TypeHandler\Factory\TypeFactory;
10
use ReflectionException;
11
12
/**
13
 * @author Milad Ghofrani <[email protected]>
14
 */
15
class TwoLevelRelationHandler extends AbstractFilterHandler implements WithRelationInterface
16
{
17
    /**
18
     * @throws ReflectionException
19
     */
20
    public function filterParameter($rootAlias, $rootClass, $relationsAndFieldName, $type): string
21
    {
22
        [$relationAlias, $subRelationAlias, $subRelationField] = $relationsAndFieldName;
23
24
        $this->validateRelationNames($relationAlias, $rootClass);
25
26
        $relationClass = $this->getRelationClass($rootClass, $relationAlias);
27
        $this->validateRelationNames($subRelationAlias, $relationClass);
28
29
        $subRelationClass = $this->getRelationClass($relationClass, $subRelationAlias);
30
        $this->validateFieldNames($subRelationField, $subRelationClass);
31
32
        $this->fieldType = $this->getFieldTypeFromAnnotation($subRelationField, $subRelationClass);
33
34
        return $this->createParameterName($subRelationAlias, $subRelationField);
35
    }
36
37
    public function filterWhereClause(
38
        $rootAlias,
39
        $relationsAndFieldName,
40
        $filterParameter,
41
        $strategy,
42
        $value
43
    ): string {
44
        [$relationAlias, $subRelationAlias, $subRelationField] = $relationsAndFieldName;
45
46
        return TypeFactory::createTypeHandler($this->fieldType)
47
            ->filterWhereClause($subRelationAlias, $subRelationField, $filterParameter, $strategy, $value)
48
        ;
49
    }
50
51
    public function relationJoin($relationJoins, $rootAlias, $rootClass, $relationsAndFieldName): array
52
    {
53
        [$relationAlias, $subRelationAlias] = $relationsAndFieldName;
54
55
        $relationJoins = $this->addRelationJoin($relationJoins, $rootAlias, $relationAlias);
56
57
        return $this->addRelationJoin($relationJoins, $relationAlias, $subRelationAlias);
58
    }
59
}
60