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
Push — master ( f788ea...49f753 )
by Cees-Jan
02:42
created

Hydrator::getAnnotation()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 14
cts 15
cp 0.9333
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
crap 3.0026
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\ApiClient\Transport;
5
6
use Doctrine\Common\Annotations\AnnotationReader;
7
use GeneratedHydrator\Configuration;
8
use ReflectionClass;
9
use WyriHaximus\ApiClient\Annotations\Nested;
10
use WyriHaximus\ApiClient\Resource\ResourceInterface;
11
use Zend\Hydrator\HydratorInterface;
12
13
class Hydrator
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $options;
19
20
    /**
21
     * @var Client
22
     */
23
    protected $transport;
24
25
    /**
26
     * @var array
27
     */
28
    protected $hydrators = [];
29
30
    /**
31
     * @var array
32
     */
33
    protected $annotations = [];
34
35
    /**
36
     * @var AnnotationReader
37
     */
38
    protected $annotationReader;
39
40
    /**
41
     * @param Client $transport
42
     * @param array $options
43
     */
44 15
    public function __construct(Client $transport, array $options)
45
    {
46 15
        $this->transport = $transport;
47 15
        $this->options = $options;
48 15
        $this->annotationReader = new AnnotationReader();
49 15
    }
50
51
    /**
52
     * @param string $class
53
     * @param array $json
54
     * @return ResourceInterface
55
     */
56 3
    public function hydrateFQCN(string $class, array $json): ResourceInterface
57
    {
58 3
        $hydrator = $this->getHydrator($class);
59 3
        $object = $this->createObject($class);
60 3
        $json = $this->hydrateNestedResources($object, $json);
61 3
        return $hydrator->hydrate($json, $object);
62
    }
63
64
    /**
65
     * @param ResourceInterface $object
66
     * @param array $json
67
     * @return array
68
     */
69 3 View Code Duplication
    protected function hydrateNestedResources(ResourceInterface $object, array $json)
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...
70
    {
71 3
        $annotation = $this->getAnnotation($object);
72
73 3
        if (!($annotation instanceof Nested)) {
74 3
            return $json;
75
        }
76
77 3
        foreach ($annotation->properties() as $property) {
78 3
            $json[$property] = $this->hydrate($annotation->get($property), $json[$property]);
79
        }
80
81 3
        return $json;
82
    }
83
84
    /**
85
     * @param string $class
86
     * @param array $json
87
     * @return ResourceInterface
88
     */
89 3
    public function hydrate(string $class, array $json): ResourceInterface
90
    {
91 3
        $fullClassName = $this->options['namespace'] . '\\' . $this->options['resource_namespace'] . '\\' . $class;
92 3
        return $this->hydrateFQCN($fullClassName, $json);
93
    }
94
95
    /**
96
     * Takes a fully qualified class name and extracts the data for that class from the given $object
97
     * @param string $class
98
     * @param ResourceInterface $object
99
     * @return array
100
     */
101 2
    public function extractFQCN(string $class, ResourceInterface $object): array
102
    {
103 2
        $json = $this->getHydrator($class)->extract($object);
104 2
        return $this->extractNestedResources($json, $object);
105
    }
106
107
    /**
108
     * @param array $json
109
     * @param ResourceInterface $object
110
     * @return array
111
     */
112 2 View Code Duplication
    protected function extractNestedResources(array $json, ResourceInterface $object)
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...
113
    {
114 2
        $annotation = $this->getAnnotation($object);
115
116 2
        if (!($annotation instanceof Nested)) {
117 2
            return $json;
118
        }
119
120 2
        foreach ($annotation->properties() as $property) {
121 2
            $json[$property] = $this->extract($annotation->get($property), $json[$property]);
122
        }
123
124 2
        return $json;
125
    }
126
127
    /**
128
     * @param ResourceInterface $object
129
     * @return null|Nested
130
     */
131 3
    protected function getAnnotation(ResourceInterface $object)
132
    {
133 3
        $class = get_class($object);
134 3
        if (isset($this->annotations[$class])) {
135 1
            return $this->annotations[$class];
136
        }
137
138 3
        $this->annotations[$class] = $this->annotationReader
139 3
            ->getClassAnnotation(
140 3
                new ReflectionClass($object),
141 3
                Nested::class
142
            )
143
        ;
144
145 3
        if ($this->annotations[$class] instanceof Nested) {
146
            return $this->annotations[$class];
147
        }
148
149 3
        $this->annotations[$class] = $this->annotationReader
150 3
            ->getClassAnnotation(
151 3
                new ReflectionClass(get_parent_class($object)),
152 3
                Nested::class
153
            )
154
        ;
155
156 3
        return $this->annotations[$class];
157
    }
158
159
    /**
160
     * @param string $class
161
     * @param ResourceInterface $object
162
     * @return array
163
     */
164 2
    public function extract(string $class, ResourceInterface $object): array
165
    {
166 2
        $fullClassName = $this->options['namespace'] . '\\' . $this->options['resource_namespace'] . '\\' . $class;
167 2
        return $this->extractFQCN($fullClassName, $object);
168
    }
169
170
    /**
171
     * @param string $resource
172
     * @param ResourceInterface $object
173
     * @return ResourceInterface
174
     */
175 1
    public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface
176
    {
177 1
        return $this->hydrateFQCN(
178 1
            $this->options['namespace'] . '\\Async\\' . $resource,
179 1
            $this->extractFQCN(
180 1
                $this->options['namespace'] . '\\Sync\\' . $resource,
181
                $object
182
            )
183
        );
184
    }
185
186
    /**
187
     * @param string $class
188
     * @return HydratorInterface
189
     */
190 3
    protected function getHydrator(string $class): HydratorInterface
191
    {
192 3
        if (isset($this->hydrators[$class])) {
193 2
            return $this->hydrators[$class];
194
        }
195
196 3
        $config = new Configuration($class);
197 3
        if (isset($this->options['resource_hydrator_cache_dir'])) {
198 3
            $config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']);
199
        }
200 3
        if (isset($this->options['resource_hydrator_namespace'])) {
201 3
            $config->setGeneratedClassesNamespace($this->options['resource_hydrator_namespace']);
202
        }
203 3
        $hydrator = $config->createFactory()->getHydratorClass();
204 3
        $this->hydrators[$class] = new $hydrator;
205
206 3
        return $this->hydrators[$class];
207
    }
208
209
    /**
210
     * @param string $class
211
     * @return ResourceInterface
212
     */
213 3
    protected function createObject(string $class): ResourceInterface
214
    {
215 3
        $object = new $class();
216 3
        $object->setTransport($this->transport);
217 3
        if (isset($this->options['setters'])) {
218
            foreach ($this->options['setters'] as $method => $argument) {
219
                if (!method_exists($object, $method)) {
220
                    continue;
221
                }
222
                $object->$method($argument);
223
            }
224
        }
225 3
        return $object;
226
    }
227
}
228