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.
Test Failed
Pull Request — 2.4 (#144)
by Alessandro
03:01
created

JoinFactory::noExistsJoin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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