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 ( 3abaaf...dcd0c7 )
by Cees-Jan
8s
created

Hydrator::buildAsyncFromSync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 6
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 73
    public function __construct(Client $transport, array $options)
20
    {
21 73
        $this->transport = $transport;
22 73
        $this->options = $options;
23 73
    }
24
25 73
    public function hydrateFQCN($class, $json): ResourceInterface
26
    {
27 73
        return $this->getHydrator($class)->hydrate($json, $this->createObject($class));
28
    }
29
30 2
    public function hydrate($class, $json): ResourceInterface
31
    {
32 2
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
33 2
        return $this->hydrateFQCN($fullClassName, $json);
34
    }
35
36
    /**
37
     * Takes a fully qualified class name and extracts the data for that class from the given $object
38
     * @param $class
39
     * @param $object
40
     * @return array
41
     */
42 2
    public function extractFQCN($class, $object): array
43
    {
44 2
        return $this->getHydrator($class)->extract($object);
45
    }
46
47 1
    public function extract($class, $object): array
48
    {
49 1
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
50 1
        return $this->extractFQCN($fullClassName, $object);
51
    }
52
53 1
    public function buildAsyncFromSync($resource, $object): ResourceInterface
54
    {
55 1
        return $this->hydrateFQCN(
56 1
            static::RESOURCE_NAMESPACE . 'Async\\' . $resource,
57 1
            $this->extractFQCN(
58 1
                static::RESOURCE_NAMESPACE . 'Sync\\' . $resource,
59
                $object
60
            )
61
        );
62
    }
63
64 73
    protected function getHydrator($class): HydratorInterface
65
    {
66 73
        if (isset($this->hydrators[$class])) {
67 1
            return $this->hydrators[$class];
68
        }
69
70 73
        $config = new Configuration($class);
71 73
        if (isset($this->options['resource_hydrator_cache_dir'])) {
72 73
            $config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']);
73
        }
74 73
        if (isset($this->options['resource_hydrator_namespace'])) {
75 73
            $config->setGeneratedClassesNamespace($this->options['resource_hydrator_namespace']);
76
        }
77 73
        $hydrator = $config->createFactory()->getHydratorClass();
78 73
        $this->hydrators[$class] = new $hydrator;
79
80 73
        return $this->hydrators[$class];
81
    }
82
83 73
    protected function createObject($class): ResourceInterface
84
    {
85 73
        $object = new $class();
86 73
        $object->setTransport($this->transport);
87 73
        return $object;
88
    }
89
}
90