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
Push — feature/refactor_and_or_filter ( c1b18b )
by Alessandro
05:08
created

JoinFactory::join()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 7.6759
c 0
b 0
f 0
cc 7
eloc 34
nc 12
nop 2

How to fix   Long Method   

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
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
        $entityAlias = $this->entityAlias;
64
65
        foreach ($relations as $relation) {
66
            $relation = $this->parser->camelize($relation);
67
            $relationEntityAlias = 'table_' . $relation;
68
69
            $metadata = $this->manager->getClassMetadata($this->entityName);
70
71
            if ($metadata->hasAssociation($relation)) {
72
                $association = $metadata->getAssociationMapping($relation);
73
74
                $fieldName = $this->parser->camelize($association['fieldName']);
75
76
                if ($this->noExistsJoin($relationEntityAlias, $relation)) {
77
                    if ($logicOperator === self::AND_OPERATOR_LOGIC) {
78
                        $param = [];
79
                        $param['field'] = $entityAlias . "." . $fieldName;
80
                        $param['relation'] = $relationEntityAlias;
81
                        $this->innerJoin[] = $param;
82
                    } elseif ($logicOperator === self::OR_OPERATOR_LOGIC) {
83
                        $param = [];
84
                        $param['field'] = $entityAlias . "." . $fieldName;
85
                        $param['relation'] = $relationEntityAlias;
86
                        $this->leftJoin[] = $param;
87
                    } else {
88
                        throw new \Exception('Missing Logic operator');
89
                    }
90
91
                    $this->storeJoin($relationEntityAlias, $relation);
92
                }
93
94
                $this->entityName = $association['targetEntity'];
95
                $entityAlias = $relationEntityAlias;
96
            }
97
98
            $this->setRelationEntityAlias($relationEntityAlias);
99
        }
100
101
        return $this;
102
    }
103
104
    private function storeJoin($prevEntityAlias, $currentEntityAlias)
105
    {
106
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
107
        $this->joins[$needle] = $needle;
108
    }
109
110
    private function noExistsJoin($prevEntityAlias, $currentEntityAlias)
111
    {
112
        if (null === $this->joins) {
113
            $this->joins = [];
114
        }
115
116
        $needle = $prevEntityAlias . '_' . $currentEntityAlias;
117
118
        return !in_array($needle, $this->joins);
119
    }
120
121
    public function getInnerJoin() :array
122
    {
123
        return $this->innerJoin;
124
    }
125
126
    public function getLeftJoin() :array
127
    {
128
        return $this->leftJoin;
129
    }
130
131
    private function setRelationEntityAlias(string $relationEntityAlias)
132
    {
133
        $this->relationEntityAlias = $relationEntityAlias;
134
    }
135
136
    public function getRelationEntityAlias()
137
    {
138
        return $this->relationEntityAlias;
139
    }
140
}