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 (#3)
by Cees-Jan
02:02
created

Hydrator::hydrateApplyAnnotations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
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
    /**
81
     * @param string $class
82
     * @param array $json
83
     * @return ResourceInterface
84
     */
85 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...
86
    {
87 4
        $fullClassName = implode(
88 4
            '\\',
89
            [
90
                $this->options[Options::NAMESPACE],
91 4
                $this->options[Options::NAMESPACE_SUFFIX],
92 4
                $class,
93
            ]
94
        );
95 4
        return $this->hydrateFQCN($fullClassName, $json);
96
    }
97
98
    /**
99
     * @param string $class
100
     * @param array $json
101
     * @return ResourceInterface
102
     */
103 4
    public function hydrateFQCN(string $class, array $json): ResourceInterface
104
    {
105 4
        $hydrator = $this->getHydrator($class);
106 4
        $object = new $class();
107 4
        $json = $this->hydrateApplyAnnotations($json, $object);
108 4
        $resource = $hydrator->hydrate($json, $object);
109 4
        if ($resource instanceof AbstractResource) {
110
            $resource->setExtraProperties($this->options[Options::EXTRA_PROPERTIES]);
111
        }
112 4
        return $resource;
113
    }
114
115
    /**
116
     * @param array $json
117
     * @param ResourceInterface $object
118
     * @return array
119
     */
120 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...
121
    {
122 4
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
123 4
            $annotation = $this->getAnnotation($object, $annotationClass);
124 4
            if ($annotation === null) {
125 4
                continue;
126
            }
127
128 4
            $json = $handler->hydrate($annotation, $json, $object);
129
        }
130
131 4
        return $json;
132
    }
133
134
    /**
135
     * @param string $class
136
     * @param ResourceInterface $object
137
     * @return array
138
     */
139 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...
140
    {
141 2
        $fullClassName = implode(
142 2
            '\\',
143
            [
144
                $this->options[Options::NAMESPACE],
145 2
                $this->options[Options::NAMESPACE_SUFFIX],
146 2
                $class,
147
            ]
148
        );
149 2
        return $this->extractFQCN($fullClassName, $object);
150
    }
151
152
    /**
153
     * Takes a fully qualified class name and extracts the data for that class from the given $object
154
     * @param string $class
155
     * @param ResourceInterface $object
156
     * @return array
157
     */
158 2
    public function extractFQCN(string $class, ResourceInterface $object): array
159
    {
160 2
        $json = $this->getHydrator($class)->extract($object);
161 2
        $json = $this->extractApplyAnnotations($object, $json);
162 2
        return $json;
163
    }
164
165
    /**
166
     * @param array $json
167
     * @param ResourceInterface $object
168
     * @return array
169
     */
170 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...
171
    {
172 2
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
173 2
            $annotation = $this->getAnnotation($object, $annotationClass);
174 2
            if ($annotation === null) {
175 2
                continue;
176
            }
177
178 2
            $json = $handler->extract($annotation, $object, $json);
179
        }
180
181 2
        return $json;
182
    }
183
184
    /**
185
     * @param ResourceInterface $object
186
     * @param string $annotationClass
187
     * @return null|AnnotationInterface
188
     */
189 4
    protected function getAnnotation(ResourceInterface $object, string $annotationClass)
190
    {
191 4
        $class = get_class($object);
192 4
        if (isset($this->annotations[$class][$annotationClass])) {
193 2
            return $this->annotations[$class][$annotationClass];
194
        }
195
196 4
        if (!isset($this->annotations[$class])) {
197 4
            $this->annotations[$class] = [];
198
        }
199
200 4
        $this->annotations[$class][$annotationClass] = $this->annotationReader
201 4
            ->getClassAnnotation(
202 4
                new ReflectionClass($object),
203
                $annotationClass
204
            )
205
        ;
206
207 4
        if (get_class($this->annotations[$class][$annotationClass]) === $annotationClass) {
208
            return $this->annotations[$class][$annotationClass];
209
        }
210
211 4
        $this->annotations[$class][$annotationClass] = $this->annotationReader
212 4
            ->getClassAnnotation(
213 4
                new ReflectionClass(get_parent_class($object)),
214
                $annotationClass
215
            )
216
        ;
217
218 4
        return $this->annotations[$class][$annotationClass];
219
    }
220
221
    /**
222
     * @param string $resource
223
     * @param ResourceInterface $object
224
     * @return ResourceInterface
225
     */
226 1
    public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface
227
    {
228 1
        return $this->hydrateFQCN(
229
            $this->options[Options::NAMESPACE] . '\\Async\\' . $resource,
230 1
            $this->extractFQCN(
231
                $this->options[Options::NAMESPACE] . '\\Sync\\' . $resource,
232
                $object
233
            )
234
        );
235
    }
236
237
    /**
238
     * @param string $class
239
     * @return HydratorInterface
240
     */
241 4
    protected function getHydrator(string $class): HydratorInterface
242
    {
243 4
        if (isset($this->hydrators[$class])) {
244 4
            return $this->hydrators[$class];
245
        }
246
247 4
        $config = new Configuration($class);
248 4
        if (isset($this->options[Options::RESOURCE_CACHE_DIR])) {
249 4
            $config->setGeneratedClassesTargetDir($this->options[Options::RESOURCE_CACHE_DIR]);
250
        }
251 4
        if (isset($this->options[Options::RESOURCE_NAMESPACE])) {
252 4
            $config->setGeneratedClassesNamespace($this->options[Options::RESOURCE_NAMESPACE]);
253
        }
254 4
        $hydrator = $config->createFactory()->getHydratorClass();
255 4
        $this->hydrators[$class] = new $hydrator;
256
257 4
        return $this->hydrators[$class];
258
    }
259
}
260