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
Pull Request — master (#62)
by Simone
02:07
created

JsonPathFinder::getPathToEntity()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Mado\QueryBundle\Component\Meta;
4
5
/**
6
 * @since Class available since Release 2.1.0
7
 */
8
class JsonPathFinder
9
{
10
    const INDEX_ENTITY_PARENT = 0;
11
12
    const INDEX_FK_RELATION_NAME = 1;
13
14
    const INDEX_ENTITY_FIRST_CHILD = 2;
15
16
    private $map;
17
18
    private $entity;
19
20
    private $entitiesPath = [];
21
22
    private $wrongPath = [];
23
24
    private $mapper;
25
26
    public function __construct(
27
        DataMapper $mapper
28
    ) {
29
        $this->mapper = $mapper;
30
    }
31
32
    public function setEntity(string $entity)
33
    {
34
        $this->entity = $entity;
35
    }
36
37
    public function getFirstParentOf(string $innerEntity)
38
    {
39
        $this->getMap();
40
41
        return $this->keep(
42
            self::INDEX_ENTITY_PARENT,
43
            $innerEntity
44
        );
45
    }
46
47
    public function getFirstChildOf(string $innerEntity)
48
    {
49
        return $this->keep(
50
            self::INDEX_ENTITY_FIRST_CHILD,
51
            $innerEntity
52
        );
53
    }
54
55
    public function getSourceRelation(string $innerEntity)
56
    {
57
        return $this->keep(
58
            self::INDEX_FK_RELATION_NAME,
59
            $innerEntity
60
        );
61
    }
62
63
    public function clearMap(string $innerEntity)
64
    {
65
        if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) {
66
            foreach ($this->map as $root => $meta) {
67
                if ($this->entity != $root) {
68
                    unset($this->map[$root]);
69
                }
70
            }
71
        }
72
    }
73
74
    public function getPathTo(string $innerEntity = '')
75
    {
76
        $this->entitiesPath[] = $innerEntity;
77
78
        $path = $this->getSourceRelation($innerEntity);
79
80
        if ($this->numberOfRelationsToEntity($innerEntity) != 1) {
81
            $this->clearMap($innerEntity);
82
        }
83
84
        if ($this->entity != $this->getFirstParentOf($innerEntity)) {
85
            if (!($relation = $this->getFirstParentOf($innerEntity))) {
86
                throw new Exceptions\UnreachablePathException(var_export([
87
                    'innerEntity' => $innerEntity,
88
                    'relation' => $relation,
89
                ], true));
90
            }
91
92
            return $this->getPathTo($relation) . '.' . $path;
93
        }
94
95
        return $path;
96
    }
97
98
    public function setQueryStartEntity(string $startEntity)
99
    {
100
        $this->setEntity($startEntity);
101
    }
102
103
    public function getPathToEntity(string $entityToReach)
104
    {
105
        foreach ($this->getMap() as $rootEntity => $meta) {
106
            if (in_array($rootEntity, $this->wrongPath)) {
107
                unset($this->map[$rootEntity]);
108
            }
109
        }
110
111
        return '_embedded.' . $this->getPathTo($entityToReach);
112
    }
113
114
    public function keep($val, $innerEntity)
115
    {
116
        foreach ($this->getMap() as $rootEntity => $meta) {
117
            foreach ($meta['relations'] as $name => $entity) {
118
                if (self::INDEX_ENTITY_FIRST_CHILD == $val) {
119
                    return $entity;
120
                }
121
122
                if ($entity == $innerEntity) {
123
                    $return = [
124
                        self::INDEX_ENTITY_PARENT      => $rootEntity,
125
                        self::INDEX_FK_RELATION_NAME   => $name,
126
                    ][$val];
127
128
                    return $return;
129
                }
130
            }
131
        }
132
    }
133
134 View Code Duplication
    public function numberOfRelationsToEntity(string $entityToReach)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        $numberOfRelationsToEntity = 0;
137
138
        foreach ($this->getMap() as $rootEntity => $meta) {
139
            foreach ($meta['relations'] as $name => $entity) {
140
                if ($entity == $entityToReach) {
141
                    $numberOfRelationsToEntity++;
142
                }
143
            }
144
        }
145
146
        return $numberOfRelationsToEntity;
147
    }
148
149 View Code Duplication
    public function listOfParentsOf(string $entityToReach)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        $parents = [];
152
153
        foreach ($this->getMap() as $rootEntity => $meta) {
154
            foreach ($meta['relations'] as $name => $entity) {
155
                if ($entity == $entityToReach) {
156
                    $parents[] = $rootEntity;
157
                }
158
            }
159
        }
160
161
        return $parents;
162
    }
163
164
    public function getEntitiesPath()
165
    {
166
        return $this->entitiesPath;
167
    }
168
169
    public function removeStep($parentToSkip)
170
    {
171
        $this->wrongPath[] = $parentToSkip;
172
    }
173
174
    public function getHashKeyForDestination(string $destination)
175
    {
176
        return md5($this->entity . $destination);
177
    }
178
179
    private function getMap()
180
    {
181
        if (!$this->map) {
182
            $this->map = $this->mapper->getMap();
183
        }
184
185
        return $this->map;
186
    }
187
}
188