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 (#12)
by Cees-Jan
01:39
created

Client::request()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0015

Importance

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