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 ( 606be7...f788ea )
by Cees-Jan
8s
created

Hydrator::getAnnotation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

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