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 ( 4c3a00...a7513d )
by Cees-Jan
02:33
created

XmlDecodeMiddleware::post()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.2464

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 12
cts 17
cp 0.7059
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 20
nc 5
nop 3
crap 8.2464
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Xml;
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\Xml\XmlDecodeService;
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 XmlDecodeMiddleware implements MiddlewareInterface
17
{
18
    use PreTrait;
19
    use ErrorTrait;
20
21
    /**
22
     * @var XmlDecodeService
23
     */
24
    private $xmlDecodeService;
25
26
    /**
27
     * @param XmlDecodeService $xmlDecodeService
28
     */
29 7
    public function __construct(XmlDecodeService $xmlDecodeService)
30
    {
31 7
        $this->xmlDecodeService = $xmlDecodeService;
32 7
    }
33
34
    /**
35
     * @param  ResponseInterface           $response
36
     * @param  array                       $options
37
     * @return CancellablePromiseInterface
38
     *
39
     * @ThirdLast()
40
     */
41 7
    public function post(
42
        ResponseInterface $response,
43
        string $transactionId,
44
        array $options = []
45
    ): CancellablePromiseInterface {
46 7
        if ($response->getBody() instanceof ReadableStreamInterface) {
47
            return resolve($response);
48
        }
49
50 7
        if (!isset($options[self::class]) &&
51 7
        strpos($response->getHeaderLine('Content-Type'), 'text/xml') !== 0) {
52 3
            return resolve($response);
53
        }
54
55 4
        if (isset($options[self::class][Options::CONTENT_TYPE]) &&
56 4
        $response->getHeaderLine('Content-Type') !== $options[self::class][Options::CONTENT_TYPE]) {
57
            return resolve($response);
58
        }
59
60 4
        $body = (string)$response->getBody();
61 4
        if ($body === '') {
62
            $stream = new BufferStream(0);
63
            $stream->write($body);
64
65
            return resolve($response->withBody($stream));
66
        }
67
68 4
        return $this->xmlDecodeService->decode($body)->then(function (array $xml) use ($response) {
69 4
            return resolve($response->withBody(new XmlStream($xml)));
70 4
        });
71
    }
72
}
73