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
03:46 queued 01:25
created

JsonPathFinder::getFirstChildOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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
        return $this->entitiesPath;
168
    }
169
170
    public function removeStep($parentToSkip)
171
    {
172
        $this->wrongPath[] = $parentToSkip;
173
    }
174
175
    public function getHashKeyForDestination(string $destination)
176
    {
177
        return md5($this->entity . $destination);
178
    }
179
180
    private function getMap()
181
    {
182
        if (!$this->map) {
183
            $this->map = $this->mapper->getMap();
184
        }
185
186
        return $this->map;
187
    }
188
189
    public function addEntity(array $parents, $rootEntity) : array
190
    {
191
        $parents[] = $rootEntity;
192
193
        return $parents;
194
    }
195
196
    public function mapTargetRelations(
197
        callable $action,
198
        $subject,
199
        string $entityToReach
200
    ) {
201
        foreach ($this->getMap() as $rootEntity => $meta) {
202
            foreach ($meta['relations'] as $name => $relationEntity) {
203
                if ($relationEntity == $entityToReach) {
204
                    $subject = $action($subject, $rootEntity);
205
                }
206
            }
207
        }
208
209
        return $subject;
210
    }
211
}
212