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:04
created

Client::hydrate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
c 3
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\Travis\Transport;
5
6
use GeneratedHydrator\Configuration;
7
use GuzzleHttp\Psr7\Request;
8
use Psr\Http\Message\ResponseInterface;
9
use React\EventLoop\LoopInterface;
10
use React\Promise\Deferred;
11
use WyriHaximus\Travis\Resource\Async;
12
use WyriHaximus\Travis\Resource\Sync;
13
14
class Client
15
{
16
    const VERSION = '0.0.1-alpha1';
17
    const USER_AGENT = 'wyrihaximus/travis-client/' . self::VERSION;
18
    const API_VERSION = 'application/vnd.travis-ci.2+json';
19
    const API_HOST_OPEN_SOURCE = 'api.travis-ci.org';
20
    const API_HOST_PRO = 'api.travis-ci.com';
21
    const API_HOST = self::API_HOST_OPEN_SOURCE;
22
    const API_SCHEMA = 'https';
23
    const RESOURCE_NAMESPACE = 'WyriHaximus\Travis\Resource\\';
24
25
    /**
26
     * @var callable
27
     */
28
    protected $handler;
29
    protected $loop;
30
    protected $options = [];
31
32
    protected $hydrators = [];
33
34
    public function __construct(LoopInterface $loop, callable $handler = null, $options = [])
35
    {
36
        $this->loop = $loop;
37
        if ($handler === null) {
38
            $handler = \Aws\default_http_handler();
39
        }
40
41
        $this->handler = $handler;
42
        $this->options = $options;
43
    }
44
45
    public function request($path)
46
    {
47
        $deferred = new Deferred();
48
        $handler = $this->handler;
49
        $handler($this->createRequest('GET', $path))->then(function (ResponseInterface $response) use ($deferred) {
50
            $deferred->resolve(json_decode($response->getBody()->getContents(), true));
51
        }, function ($error) use ($deferred) {
52
            $deferred->reject($error);
53
        });
54
        return $deferred->promise();
55
    }
56
57
    public function hydrateFQCN($class, $json)
58
    {
59
        $object = new $class();
60
        $object->setTransport($this);
61
        return $this->getHydrator($class)->hydrate($json, $object);
62
    }
63
64
    public function hydrate($class, $json)
65
    {
66
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
67
        $object = new $fullClassName();
68
        $object->setTransport($this);
69
        return $this->getHydrator($fullClassName)->hydrate($json, $object);
70
    }
71
72
    public function extractFQCN($class, $object)
73
    {
74
        return $this->getHydrator($class)->extract($object);
75
    }
76
77
    public function extract($class, $object)
78
    {
79
        $fullClassName = self::RESOURCE_NAMESPACE . $this->options['resource_namespace'] . '\\' . $class;
80
        return $this->getHydrator($fullClassName)->extract($object);
81
    }
82
83
    public function buildAsyncFromSync($resource, $object)
84
    {
85
        return $this->hydrateFQCN(
86
            static::RESOURCE_NAMESPACE . 'Async\\' . $resource,
87
            $this->extractFQCN(
88
                static::RESOURCE_NAMESPACE . 'Sync\\' . $resource,
89
                $object
90
            )
91
        );
92
    }
93
94
    protected function getHydrator($class)
95
    {
96
        if (isset($this->hydrators[$class])) {
97
            return $this->hydrators[$class];
98
        }
99
100
        $config = new Configuration($class);
101
        if (isset($this->options['resource_hydrator_cache_dir'])) {
102
            $config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']);
103
        }
104
        $hydrator = $config->createFactory()->getHydratorClass();
105
        $this->hydrators[$class] = new $hydrator;
106
        return $this->hydrators[$class];
107
    }
108
109
    protected function createRequest(string $method, string $path)
110
    {
111
        $url = self::API_SCHEMA . '://' . self::API_HOST . '/' . $path;
112
        return new Request($method, $url, [
113
            'User-Agent' => self::USER_AGENT,
114
            'Accept' => self::API_VERSION,
115
        ]);
116
    }
117
118
    /**
119
     * @return LoopInterface
120
     */
121
    public function getLoop()
122
    {
123
        return $this->loop;
124
    }
125
}
126