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.
Completed
Pull Request — master (#4)
by Cees-Jan
02:32
created

Hydrator::preheat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Hydrator;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\Common\Annotations\CachedReader;
7
use Doctrine\Common\Annotations\Reader;
8
use Doctrine\Common\Cache\Cache;
9
use GeneratedHydrator\Configuration;
10
use ReflectionClass;
11
use ApiClients\Foundation\Resource\ResourceInterface;
12
use ApiClients\Foundation\Resource\AbstractResource;
13
use Zend\Hydrator\HydratorInterface;
14
15
class Hydrator
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $options;
21
22
    /**
23
     * @var array
24
     */
25
    protected $hydrators = [];
26
27
    /**
28
     * @var array
29
     */
30
    protected $annotations = [];
31
32
    /**
33
     * @var HandlerInterface[]
34
     */
35
    protected $annotationHandlers = [];
36
37
    /**
38
     * @var Reader
39
     */
40
    protected $annotationReader;
41
42
    /**
43
     * @param array $options
44
     */
45 4
    public function __construct(array $options)
46
    {
47 4
        $this->options = $options;
48
49 4
        $reader = new AnnotationReader();
50 4
        if (isset($this->options[Options::ANNOTATION_CACHE]) &&
51 4
            $this->options[Options::ANNOTATION_CACHE] instanceof Cache
52
        ) {
53 1
            $reader = new CachedReader(
54
                $reader,
55 1
                $this->options[Options::ANNOTATION_CACHE]
56
            );
57
        }
58 4
        $this->annotationReader = $reader;
59
60 4
        $this->setUpAnnotations();
61 4
        $this->addSelfToExtraProperties();
62 4
    }
63
64 4
    protected function setUpAnnotations()
65
    {
66 4
        if (!isset($this->options[Options::ANNOTATIONS])) {
67
            return;
68
        }
69
70 4
        foreach ($this->options[Options::ANNOTATIONS] as $annotationClass => $handler) {
71 4
            $this->annotationHandlers[$annotationClass] = new $handler($this);
72
        }
73 4
    }
74
75 4
    protected function addSelfToExtraProperties()
76
    {
77 4
        $this->options[Options::EXTRA_PROPERTIES]['hydrator'] = $this;
78 4
    }
79
80
    public function preheat()
81
    {
82
        // TODO
83
    }
84
85
    /**
86
     * @param string $class
87
     * @param array $json
88
     * @return ResourceInterface
89
     */
90 4 View Code Duplication
    public function hydrate(string $class, array $json): ResourceInterface
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...
91
    {
92 4
        $fullClassName = implode(
93 4
            '\\',
94
            [
95
                $this->options[Options::NAMESPACE],
96 4
                $this->options[Options::NAMESPACE_SUFFIX],
97 4
                $class,
98
            ]
99
        );
100 4
        return $this->hydrateFQCN($fullClassName, $json);
101
    }
102
103
    /**
104
     * @param string $class
105
     * @param array $json
106
     * @return ResourceInterface
107
     */
108 4
    public function hydrateFQCN(string $class, array $json): ResourceInterface
109
    {
110 4
        $hydrator = $this->getHydrator($class);
111 4
        $object = new $class();
112 4
        $json = $this->hydrateApplyAnnotations($json, $object);
113 4
        $resource = $hydrator->hydrate($json, $object);
114 4
        if ($resource instanceof AbstractResource) {
115
            $resource->setExtraProperties($this->options[Options::EXTRA_PROPERTIES]);
116
        }
117 4
        return $resource;
118
    }
119
120
    /**
121
     * @param array $json
122
     * @param ResourceInterface $object
123
     * @return array
124
     */
125 4 View Code Duplication
    protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array
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...
126
    {
127 4
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
128 4
            $annotation = $this->getAnnotation($object, $annotationClass);
129 4
            if ($annotation === null) {
130 4
                continue;
131
            }
132
133 4
            $json = $handler->hydrate($annotation, $json, $object);
134
        }
135
136 4
        return $json;
137
    }
138
139
    /**
140
     * @param string $class
141
     * @param ResourceInterface $object
142
     * @return array
143
     */
144 2 View Code Duplication
    public function extract(string $class, ResourceInterface $object): array
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...
145
    {
146 2
        $fullClassName = implode(
147 2
            '\\',
148
            [
149
                $this->options[Options::NAMESPACE],
150 2
                $this->options[Options::NAMESPACE_SUFFIX],
151 2
                $class,
152
            ]
153
        );
154 2
        return $this->extractFQCN($fullClassName, $object);
155
    }
156
157
    /**
158
     * Takes a fully qualified class name and extracts the data for that class from the given $object
159
     * @param string $class
160
     * @param ResourceInterface $object
161
     * @return array
162
     */
163 2
    public function extractFQCN(string $class, ResourceInterface $object): array
164
    {
165 2
        $json = $this->getHydrator($class)->extract($object);
166 2
        $json = $this->extractApplyAnnotations($object, $json);
167 2
        return $json;
168
    }
169
170
    /**
171
     * @param array $json
172
     * @param ResourceInterface $object
173
     * @return array
174
     */
175 2 View Code Duplication
    protected function extractApplyAnnotations(ResourceInterface $object, array $json): array
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...
176
    {
177 2
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
178 2
            $annotation = $this->getAnnotation($object, $annotationClass);
179 2
            if ($annotation === null) {
180 2
                continue;
181
            }
182
183 2
            $json = $handler->extract($annotation, $object, $json);
184
        }
185
186 2
        return $json;
187
    }
188
189
    /**
190
     * @param ResourceInterface $object
191
     * @param string $annotationClass
192
     * @return null|AnnotationInterface
193
     */
194 4
    protected function getAnnotation(ResourceInterface $object, string $annotationClass)
195
    {
196 4
        $class = get_class($object);
197 4
        if (isset($this->annotations[$class][$annotationClass])) {
198 2
            return $this->annotations[$class][$annotationClass];
199
        }
200
201 4
        if (!isset($this->annotations[$class])) {
202 4
            $this->annotations[$class] = [];
203
        }
204
205 4
        $this->annotations[$class][$annotationClass] = $this->annotationReader
206 4
            ->getClassAnnotation(
207 4
                new ReflectionClass($object),
208
                $annotationClass
209
            )
210
        ;
211
212 4
        if (get_class($this->annotations[$class][$annotationClass]) === $annotationClass) {
213
            return $this->annotations[$class][$annotationClass];
214
        }
215
216 4
        $this->annotations[$class][$annotationClass] = $this->annotationReader
217 4
            ->getClassAnnotation(
218 4
                new ReflectionClass(get_parent_class($object)),
219
                $annotationClass
220
            )
221
        ;
222
223 4
        return $this->annotations[$class][$annotationClass];
224
    }
225
226
    /**
227
     * @param string $resource
228
     * @param ResourceInterface $object
229
     * @return ResourceInterface
230
     */
231 1
    public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface
232
    {
233 1
        return $this->hydrateFQCN(
234
            $this->options[Options::NAMESPACE] . '\\Async\\' . $resource,
235 1
            $this->extractFQCN(
236
                $this->options[Options::NAMESPACE] . '\\Sync\\' . $resource,
237
                $object
238
            )
239
        );
240
    }
241
242
    /**
243
     * @param string $class
244
     * @return HydratorInterface
245
     */
246 4
    protected function getHydrator(string $class): HydratorInterface
247
    {
248 4
        if (isset($this->hydrators[$class])) {
249 4
            return $this->hydrators[$class];
250
        }
251
252 4
        $config = new Configuration($class);
253 4
        if (isset($this->options[Options::RESOURCE_CACHE_DIR])) {
254 4
            $config->setGeneratedClassesTargetDir($this->options[Options::RESOURCE_CACHE_DIR]);
255
        }
256 4
        if (isset($this->options[Options::RESOURCE_NAMESPACE])) {
257 4
            $config->setGeneratedClassesNamespace($this->options[Options::RESOURCE_NAMESPACE]);
258
        }
259 4
        $hydrator = $config->createFactory()->getHydratorClass();
260 4
        $this->hydrators[$class] = new $hydrator;
261
262 4
        return $this->hydrators[$class];
263
    }
264
}
265