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.

CustomRequestBodyParsers   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
c 2
b 0
f 0
dl 0
loc 66
ccs 30
cts 33
cp 0.9091
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addType() 0 3 1
A __invoke() 0 18 3
A __construct() 0 34 3
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\React\Http\Middleware;
4
5
use Psr\Http\Message\ServerRequestInterface;
6
use function React\Promise\reject;
7
use function RingCentral\Psr7\stream_for;
8
9
final class CustomRequestBodyParsers
10
{
11
    /**
12
     * @var array
13
     */
14
    private $types = [];
15
16 6
    public function __construct()
17
    {
18
        /**
19
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
20
         */
21
        $this->addType('application/json', function (ServerRequestInterface $request) {
22 1
            $body = (string)$request->getBody();
23 1
            $result = \json_decode($body, true);
24 1
            if (!\is_array($result)) {
25
                return $request;
26
            }
27
28 1
            return $request->withParsedBody($result)->withBody(stream_for($body));
29 6
        });
30
31
        /**
32
         * Via: https://github.com/reactphp/http/pull/220#discussion_r140863176.
33
         */
34
        $xmlParser = function (ServerRequestInterface $request) {
35 3
            $body = (string)$request->getBody();
36 3
            $backup = \libxml_disable_entity_loader(true);
37 3
            $backup_errors = \libxml_use_internal_errors(true);
38 3
            $result = \simplexml_load_string($body);
39 3
            \libxml_disable_entity_loader($backup);
40 3
            \libxml_clear_errors();
41 3
            \libxml_use_internal_errors($backup_errors);
42 3
            if ($result === false) {
43
                return $request;
44
            }
45
46 3
            return $request->withParsedBody($result)->withBody(stream_for($body));
47 6
        };
48 6
        $this->addType('application/xml', $xmlParser);
49 6
        $this->addType('text/xml', $xmlParser);
50 6
    }
51
52 6
    public function __invoke(ServerRequestInterface $request, $next)
53
    {
54 6
        $type = \strtolower($request->getHeaderLine('Content-Type'));
55 6
        list($type) = \explode(';', $type);
56
57 6
        if (!isset($this->types[$type])) {
58
            return $next($request);
59
        }
60
61
        try {
62 6
            $parser = $this->types[$type];
63
            /** @var ServerRequestInterface $request */
64 6
            $request = $parser($request);
65 1
        } catch (\Throwable $t) {
66 1
            return reject($t);
67
        }
68
69 5
        return $next($request);
70
    }
71
72 6
    public function addType($type, $callback): void
73
    {
74 6
        $this->types[$type] = $callback;
75 6
    }
76
}
77