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   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 75%
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 70
ccs 24
cts 32
cp 0.75
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hydrateFQCN() 0 4 1
A hydrate() 0 5 1
A extractFQCN() 0 4 1
A extract() 0 5 1
A buildAsyncFromSync() 0 10 1
A getHydrator() 0 15 3
A createObject() 0 6 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