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 ( 27c623...2425d7 )
by Cees-Jan
04:45 queued 02:56
created

Client::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1.0007

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
crap 1.0007
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Transport;
4
5
use ApiClients\Foundation\Middleware\Locator\Locator;
6
use ApiClients\Foundation\Middleware\MiddlewareRunner;
7
use Clue\React\Buzz\Browser;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use React\EventLoop\LoopInterface;
11
use React\Promise\PromiseInterface;
12
use RingCentral\Psr7\Uri;
13
use Throwable;
14
use function React\Promise\reject;
15
use function React\Promise\resolve;
16
17
final class Client implements ClientInterface
18
{
19
    const DEFAULT_OPTIONS = [
20
        Options::SCHEMA => 'https',
21
        Options::PATH => '/',
22
        Options::HEADERS => [],
23
    ];
24
25
    /**
26
     * @var LoopInterface
27
     */
28
    protected $loop;
29
30
    /**
31
     * @var Locator
32
     */
33
    protected $locator;
34
35
    /**
36
     * @var Browser
37
     */
38
    protected $browser;
39
40
    /**
41
     * @var array
42
     */
43
    protected $options = [];
44
45
    /**
46
     * @var string[]
47
     */
48
    protected $middleware = [];
49
50
    /**
51
     * @param LoopInterface $loop
52
     * @param Locator       $locator
53
     * @param Browser       $buzz
54
     * @param array         $options
55
     */
56 17
    public function __construct(
57
        LoopInterface $loop,
58
        Locator $locator,
59
        Browser $buzz,
60
        array $options = []
61
    ) {
62 17
        $this->loop = $loop;
63 17
        $this->locator = $locator;
64 17
        $this->browser = $buzz;
65 17
        $this->options = $options + self::DEFAULT_OPTIONS;
66
67 17
        if (isset($this->options[Options::MIDDLEWARE])) {
68 16
            $this->middleware = $this->options[Options::MIDDLEWARE];
69
        }
70 17
    }
71
72
    /**
73
     * @param  RequestInterface $request
74
     * @param  array            $options
75
     * @return PromiseInterface
76
     */
77 16
    public function request(RequestInterface $request, array $options = []): PromiseInterface
78
    {
79 16
        $options = $this->applyRequestOptions($options);
80 16
        $request = $this->applyApiSettingsToRequest($request, $options);
81 16
        $executioner = $this->constructMiddlewares($options);
82
83
        return $executioner->pre($request)->then(function ($request) use ($options) {
84 16
            return resolve($this->browser->send(
85 16
                $request
86
            ));
87
        }, function (ResponseInterface $response) {
88
            return resolve($response);
89
        })->then(function (ResponseInterface $response) use ($executioner) {
90 8
            return $executioner->post($response);
91 16
        })->otherwise(function (Throwable $throwable) use ($executioner) {
92 8
            return reject($executioner->error($throwable));
93 16
        });
94
    }
95
96 16
    public function applyRequestOptions(array $options): array
97
    {
98 16
        if (!isset($this->options[Options::DEFAULT_REQUEST_OPTIONS])) {
99 16
            return $options;
100
        }
101
102
        return array_merge_recursive(
103
            $this->options[Options::DEFAULT_REQUEST_OPTIONS],
104
            $options
105
        );
106
    }
107
108 16
    protected function constructMiddlewares(array $options): MiddlewareRunner
109
    {
110 16
        $set = $this->middleware;
111
112 16
        if (isset($options[Options::MIDDLEWARE])) {
113
            $set = $this->combinedMiddlewares($options[Options::MIDDLEWARE]);
114
        }
115
116 16
        $args = [];
117 16
        $args[] = $options;
118 16
        foreach ($set as $middleware) {
119 16
            $args[] = $this->locator->get($middleware);
120
        }
121
122 16
        return new MiddlewareRunner(...$args);
123
    }
124
125
    protected function combinedMiddlewares(array $extraMiddlewares): array
126
    {
127
        $set = $this->middleware;
128
129
        foreach ($extraMiddlewares as $middleware) {
130
            if (in_array($middleware, $set, true)) {
131
                continue;
132
            }
133
134
            $set[] = $middleware;
135
        }
136
137
        return $set;
138
    }
139
140 16
    protected function applyApiSettingsToRequest(RequestInterface $request, array $options): RequestInterface
141
    {
142 16
        $options = array_replace_recursive($this->options, $options);
143 16
        $uri = $request->getUri();
144 16
        if (strpos((string)$uri, '://') === false) {
145 4
            $uri = Uri::resolve(
146 4
                new Uri(
147 4
                    $options[Options::SCHEMA] .
148 4
                    '://' .
149 4
                    $options[Options::HOST] .
150 4
                    $options[Options::PATH]
151
                ),
152 4
                $request->getUri()
153
            );
154
        }
155
156 16
        foreach ($options[Options::HEADERS] as $key => $value) {
157 10
            $request = $request->withAddedHeader($key, $value);
158
        }
159
160 16
        return $request->withUri($uri);
161
    }
162
}
163