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 ( 2715b9...95b2cc )
by Cees-Jan
11s
created

JsonDecodeMiddleware::post()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.5375

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 14
cts 18
cp 0.7778
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 21
nc 5
nop 3
crap 7.5375
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Json;
4
5
use ApiClients\Foundation\Middleware\Annotation\ThirdLast;
6
use ApiClients\Foundation\Middleware\ErrorTrait;
7
use ApiClients\Foundation\Middleware\MiddlewareInterface;
8
use ApiClients\Foundation\Middleware\PreTrait;
9
use ApiClients\Tools\Json\JsonDecodeService;
10
use GuzzleHttp\Psr7\BufferStream;
11
use Psr\Http\Message\ResponseInterface;
12
use React\Promise\CancellablePromiseInterface;
13
use React\Stream\ReadableStreamInterface;
14
use function React\Promise\resolve;
15
16
class JsonDecodeMiddleware implements MiddlewareInterface
17
{
18
    use PreTrait;
19
    use ErrorTrait;
20
21
    /**
22
     * @var JsonDecodeService
23
     */
24
    private $jsonDecodeService;
25
26
    /**
27
     * JsonDecode constructor.
28
     * @param JsonDecodeService $jsonDecodeService
29
     */
30 6
    public function __construct(JsonDecodeService $jsonDecodeService)
31
    {
32 6
        $this->jsonDecodeService = $jsonDecodeService;
33 6
    }
34
35
    /**
36
     * @param ResponseInterface $response
37
     * @param array $options
38
     * @return CancellablePromiseInterface
39
     *
40
     * @ThirdLast()
41
     */
42 6
    public function post(
43
        ResponseInterface $response,
44
        string $transactionId,
45
        array $options = []
46
    ): CancellablePromiseInterface {
47 6
        if ($response->getBody() instanceof ReadableStreamInterface) {
48 1
            return resolve($response);
49
        }
50
51 5
        if (!isset($options[self::class]) &&
52 5
            $response->getHeaderLine('Content-Type') !== 'application/json') {
53 2
            return resolve($response);
54
        }
55
56 3
        if (isset($options[self::class][Options::CONTENT_TYPE]) &&
57 3
            $response->getHeaderLine('Content-Type') !== $options[self::class][Options::CONTENT_TYPE]) {
58
            return resolve($response);
59
        }
60
61 3
        $body = (string)$response->getBody();
62 3
        if ($body === '') {
63
            $stream = new BufferStream(0);
64
            $stream->write($body);
65
            return resolve($response->withBody($stream));
66
        }
67
68 3
        return $this->jsonDecodeService->decode($body)->then(function ($json) use ($response) {
69 3
            $body = new JsonStream($json);
70 3
            return resolve($response->withBody($body));
71 3
        });
72
    }
73
}
74