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:20
created

JsonPathFinder::keep()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 15
rs 8.8571
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
    private $appendRootEntityToSubject;
27
28
    private $incrementSubject;
29
30
    public function __construct(
31
        DataMapper $mapper
32
    ) {
33
        $this->mapper = $mapper;
34
35
        $this->appendRootEntityToSubject =  function($subject, $rootEntity) {
36
            $subject[] = $rootEntity;
37
            return $subject;
38
        };
39
40
        $this->incrementSubject = function($subject) {
41
            return ++$subject;
42
        };
43
    }
44
45
    public function setEntity(string $entity)
46
    {
47
        $this->entity = $entity;
48
    }
49
50
    public function getFirstParentOf(string $innerEntity)
51
    {
52
        $this->getMap();
53
54
        return $this->keep(
55
            self::INDEX_ENTITY_PARENT,
56
            $innerEntity
57
        );
58
    }
59
60
    public function getFirstChildOf(string $innerEntity)
61
    {
62
        return $this->keep(
63
            self::INDEX_ENTITY_FIRST_CHILD,
64
            $innerEntity
65
        );
66
    }
67
68
    public function getSourceRelation(string $innerEntity)
69
    {
70
        return $this->keep(
71
            self::INDEX_FK_RELATION_NAME,
72
            $innerEntity
73
        );
74
    }
75
76
    public function clearMap(string $innerEntity)
77
    {
78
        if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) {
79
            foreach ($this->map as $rootEntity => $meta) {
80
                if ($this->entity != $rootEntity) {
81
                    unset($this->map[$rootEntity]);
82
                }
83
            }
84
        }
85
    }
86
87
    public function getPathTo(string $innerEntity = '')
88
    {
89
        $this->entitiesPath[] = $innerEntity;
90
91
        $path = $this->getSourceRelation($innerEntity);
92
93
        if ($this->numberOfRelationsToEntity($innerEntity) != 1) {
94
            $this->clearMap($innerEntity);
95
        }
96
97
        if ($this->entity != $this->getFirstParentOf($innerEntity)) {
98
            if (!($relation = $this->getFirstParentOf($innerEntity))) {
99
                throw new Exceptions\UnreachablePathException(var_export([
100
                    'innerEntity' => $innerEntity,
101
                    'relation' => $relation,
102
                ], true));
103
            }
104
105
            return $this->getPathTo($relation) . '.' . $path;
106
        }
107
108
        return $path;
109
    }
110
111
    public function setQueryStartEntity(string $startEntity)
112
    {
113
        $this->setEntity($startEntity);
114
    }
115
116
    public function getPathToEntity(string $entityToReach)
117
    {
118
        foreach ($this->getMap() as $rootEntity => $meta) {
119
            if (in_array($rootEntity, $this->wrongPath)) {
120
                unset($this->map[$rootEntity]);
121
            }
122
        }
123
124
        return '_embedded.' . $this->getPathTo($entityToReach);
125
    }
126
127
    public function keep($val, $innerEntity)
128
    {
129
        foreach ($this->getMap() as $rootEntity => $meta) {
130
            foreach ($meta['relations'] as $name => $entity) {
131
                if (self::INDEX_ENTITY_FIRST_CHILD == $val) {
132
                    return $entity;
133
                }
134
135
                if ($entity == $innerEntity) {
136
                    $return = [
137
                        self::INDEX_ENTITY_PARENT      => $rootEntity,
138
                        self::INDEX_FK_RELATION_NAME   => $name,
139
                    ][$val];
140
141
                    return $return;
142
                }
143
            }
144
        }
145
    }
146
147
    public function numberOfRelationsToEntity(string $entityToReach)
148
    {
149
        return $this->mapTargetRelations(
150
            $this->incrementSubject,
151
            $subject = 0,
152
            $entityToReach
153
        );
154
    }
155
156
    public function listOfParentsOf(string $entityToReach)
157
    {
158
        return $this->mapTargetRelations(
159
            $this->appendRootEntityToSubject,
160
            $subject = [],
161
            $entityToReach
162
        );
163
    }
164
165
    public function getEntitiesPath()
166
    {
167
        if (!$this->entitiesPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entitiesPath of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
168
            throw new Exceptions\UndefinedPathException(
169
                'Any path was requested'
170
            );
171
        }
172
173
        return $this->entitiesPath;
174
    }
175
176
    public function removeStep($parentToSkip)
177
    {
178
        $this->wrongPath[] = $parentToSkip;
179
    }
180
181
    public function getHashKeyForDestination(string $destination)
182
    {
183
        return md5($this->entity . $destination);
184
    }
185
186
    private function getMap()
187
    {
188
        if (!$this->map) {
189
            $this->map = $this->mapper->getMap();
190
        }
191
192
        return $this->map;
193
    }
194
195
    public function addEntity(array $parents, $rootEntity) : array
196
    {
197
        $parents[] = $rootEntity;
198
199
        return $parents;
200
    }
201
202
    public function mapTargetRelations(
203
        callable $action,
204
        $subject,
205
        string $entityToReach
206
    ) {
207
        foreach ($this->getMap() as $rootEntity => $meta) {
208
            foreach ($meta['relations'] as $name => $relationEntity) {
209
                if ($relationEntity == $entityToReach) {
210
                    $subject = $action($subject, $rootEntity);
211
                }
212
            }
213
        }
214
215
        return $subject;
216
    }
217
}
218