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

Hydrator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\Travis\Transport;
5
6
use GeneratedHydrator\Configuration;
7
use WyriHaximus\Travis\Resource\RepositoryInterface;
8
use WyriHaximus\Travis\Resource\ResourceInterface;
9
use Zend\Hydrator\HydratorInterface;
10
11
class Hydrator
12
{
13
    const RESOURCE_NAMESPACE = 'WyriHaximus\Travis\Resource\\';
14
15
    protected $options;
16
    protected $transport;
17
    protected $hydrators = [];
18
19 47
    public function __construct(Client $transport, array $options)
20
    {
21 47
        $this->transport = $transport;
22 47
        $this->options = $options;
23 47
    }
24
25 47
    public function hydrateFQCN($class, $json): ResourceInterface
26
    {
27 47
        return $this->getHydrator($class)->hydrate($json, $this->createObject($class));
28
    }
29
30
    public function hydrate($class, $json): ResourceInterface
31
    {
32
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
33
        return $this->hydrateFQCN($fullClassName, $json);
34
    }
35
36 1
    public function extractFQCN($class, $object): array
37
    {
38 1
        return $this->getHydrator($class)->extract($object);
39
    }
40
41
    public function extract($class, $object): array
42
    {
43
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
44
        return $this->extractFQCN($fullClassName, $object);
45
    }
46
47 1
    public function buildAsyncFromSync($resource, $object): RepositoryInterface
48
    {
49 1
        return $this->hydrateFQCN(
50 1
            static::RESOURCE_NAMESPACE . 'Async\\' . $resource,
51 1
            $this->extractFQCN(
52 1
                static::RESOURCE_NAMESPACE . 'Sync\\' . $resource,
53
                $object
54
            )
55
        );
56
    }
57
58 47
    protected function getHydrator($class): HydratorInterface
59
    {
60 47
        if (isset($this->hydrators[$class])) {
61
            return $this->hydrators[$class];
62
        }
63
64 47
        $config = new Configuration($class);
65 47
        if (isset($this->options['resource_hydrator_cache_dir'])) {
66
            $config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']);
67
        }
68 47
        $hydrator = $config->createFactory()->getHydratorClass();
69 47
        $this->hydrators[$class] = new $hydrator;
70
71 47
        return $this->hydrators[$class];
72
    }
73
74 47
    protected function createObject($class): ResourceInterface
75
    {
76 47
        $object = new $class();
77 47
        $object->setTransport($this->transport);
78 47
        return $object;
79
    }
80
}
81