GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — feature/refactor_and_or_filter ( ca568a...4a7062 )
by Alessandro
03:12
created

JoinFactory::getLeftJoin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Mado\QueryBundle\Queries;
4
5
use Doctrine\ORM\EntityManager;
6
use Mado\QueryBundle\Services\StringParser;
7
8
class JoinFactory
9
{
10
    private const AND_OPERATOR_LOGIC = 'AND';
11
12
    private const OR_OPERATOR_LOGIC = 'OR';
13
14
    private $parser;
15
16
    private $entityName;
17
18
    private $entityAlias;
19
20
    private $manager;
21
22
    private $innerJoin;
23
24
    private $leftJoin;
25
26
    private $joins;
27
28
    private $relationEntityAlias;
29
30 25
    public function __construct(string $entityName, string $entityAlias, EntityManager $manager)
31
    {
32 25
        $this->parser  = new StringParser();
33 25
        $this->entityName = $entityName;
34 25
        $this->entityAlias = $entityAlias;
35 25
        $this->manager = $manager;
36 25
        $this->innerJoin = [];
37 25
        $this->leftJoin = [];
38 25
        $joins = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $joins is dead and can be removed.
Loading history...
39 25
    }
40
41
    /**
42
     * @param String $relation Nome della relazione semplice (groups.name) o con embedded (_embedded.groups.name)
43
     * @return $this
44
     */
45 11
    public function join(String $relation, $logicOperator = self::AND_OPERATOR_LOGIC)
46
    {
47 11
        $relation = explode('|', $relation)[0];
48 11
        $relations = [$relation];
49
50 11
        if (strstr($relation, '_embedded.')) {
51 11
            $embeddedFields = explode('.', $relation);
52 11
            $this->parser->camelize($embeddedFields[1]);
53
54
            // elimino l'ultimo elemento che dovrebbe essere il nome del campo
55 11
            unset($embeddedFields[count($embeddedFields) - 1]);
56
57
            // elimino il primo elemento _embedded
58 11
            unset($embeddedFields[0]);
59
60 11
            $relations = $embeddedFields;
61
        }
62
63 11
        $entityName = $this->entityName;
64 11
        $entityAlias = $this->entityAlias;
65
66 11
        foreach ($relations as $relation) {
67 11
            $relation = $this->parser->camelize($relation);
68 11
            $relationEntityAlias = 'table_' . $relation;
69
70 11
            $metadata = $this->manager->getClassMetadata($entityName);
71
72 11
            if ($metadata->hasAssociation($relation)) {
73 11
                $association = $metadata->getAssociationMapping($relation);
74
75 11
                $fieldName = $this->parser->camelize($association['fieldName']);
76
77 11
                if ($this->noExistsJoin($relationEntityAlias, $relation)) {
78 11
                    if ($logicOperator === self::AND_OPERATOR_LOGIC) {
79 6
                        $param = [];
80 6
                        $param['field'] = $entityAlias . "." . $fieldName;
81 6
                        $param['relation'] = $relationEntityAlias;
82 6
                        $this->innerJoin[] = $param;
83 5
                    } elseif ($logicOperator === self::OR_OPERATOR_LOGIC) {
84 5
                        $param = [];
85 5
                        $param['field'] = $entityAlias . "." . $fieldName;
86 5
                        $param['relation'] = $relationEntityAlias;
87 5
                        $this->leftJoin[] = $param;
88
                    } else {
89
                        throw new \Exception('Missing Logic operator');
90
                    }
91
92 11
                    $this->storeJoin($relationEntityAlias, $relation);
93
                }
94
95 11
                $entityName = $association['targetEntity'];
96 11
                $entityAlias = $relationEntityAlias;
97
            }
98
99 11
            $this->setRelationEntityAlias($relationEntityAlias);
100
        }
101
102 11
        return $this;
103
    }
104
105 11
    private function storeJoin($prevEntityAlias, $currentEntityAlias)
106
    {
107 11
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
108 11
        $this->joins[$needle] = $needle;
109 11
    }
110
111 11
    private function noExistsJoin($prevEntityAlias, $currentEntityAlias)
112
    {
113 11
        if (null === $this->joins) {
114 11
            $this->joins = [];
115
        }
116
117 11
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
118
119 11
        return !in_array($needle, $this->joins);
120
    }
121
122 13
    public function getInnerJoin() :array
123
    {
124 13
        return $this->innerJoin;
125
    }
126
127 12
    public function getLeftJoin() :array
128
    {
129 12
        return $this->leftJoin;
130
    }
131
132 11
    private function setRelationEntityAlias(string $relationEntityAlias)
133
    {
134 11
        $this->relationEntityAlias = $relationEntityAlias;
135 11
    }
136
137 11
    public function getRelationEntityAlias()
138
    {
139 11
        return $this->relationEntityAlias;
140
    }
141
}