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

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
lcom 2
cbo 6
dl 0
loc 101
ccs 0
cts 43
cp 0
rs 10
c 3
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A request() 0 11 1
A hydrateFQCN() 0 6 1
A hydrate() 0 7 1
A extractFQCN() 0 4 1
A extract() 0 5 1
A getHydrator() 0 14 3
A createRequest() 0 8 1
A getLoop() 0 4 1
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
    protected function getHydrator($class)
84
    {
85
        if (isset($this->hydrators[$class])) {
86
            return $this->hydrators[$class];
87
        }
88
89
        $config = new Configuration($class);
90
        if (isset($this->options['resource_hydrator_cache_dir'])) {
91
            $config->setGeneratedClassesTargetDir($this->options['resource_hydrator_cache_dir']);
92
        }
93
        $hydrator = $config->createFactory()->getHydratorClass();
94
        $this->hydrators[$class] = new $hydrator;
95
        return $this->hydrators[$class];
96
    }
97
98
    protected function createRequest(string $method, string $path)
99
    {
100
        $url = self::API_SCHEMA . '://' . self::API_HOST . '/' . $path;
101
        return new Request($method, $url, [
102
            'User-Agent' => self::USER_AGENT,
103
            'Accept' => self::API_VERSION,
104
        ]);
105
    }
106
107
    /**
108
     * @return LoopInterface
109
     */
110
    public function getLoop()
111
    {
112
        return $this->loop;
113
    }
114
}
115