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.

Join::join()   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 7.0011

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 58
c 0
b 0
f 0
ccs 34
cts 35
cp 0.9714
rs 8.4266
cc 7
nc 12
nop 2
crap 7.0011

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