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 ( 245b97...a80bec )
by Cees-Jan
08:40
created

ErrorMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B error() 0 39 5
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Client\Github\Middleware;
4
5
use ApiClients\Client\Github\ApiErrorException;
6
use ApiClients\Foundation\Middleware\Annotation\SecondLast;
7
use ApiClients\Foundation\Middleware\MiddlewareInterface;
8
use ApiClients\Foundation\Middleware\PostTrait;
9
use ApiClients\Foundation\Middleware\PreTrait;
10
use ApiClients\Foundation\Transport\ParsedContentsInterface;
11
use ApiClients\Tools\Psr7\HttpStatusExceptions\BadRequestException;
12
use ApiClients\Tools\Psr7\HttpStatusExceptions\UnprocessableEntityException;
13
use React\Promise\CancellablePromiseInterface;
14
use Throwable;
15
use function React\Promise\reject;
16
17
final class ErrorMiddleware implements MiddlewareInterface
18
{
19
    use PreTrait;
20
    use PostTrait;
21
22
    /**
23
     * @param Throwable $throwable
24
     * @param string $transactionId
25
     * @param array $options
26
     * @return CancellablePromiseInterface
27
     * @SecondLast()
28
     */
29
    public function error(
30
        Throwable $throwable,
31
        string $transactionId,
32
        array $options = []
33
    ): CancellablePromiseInterface
34
    {
35
        if ($throwable instanceof UnprocessableEntityException ||
36
            $throwable instanceof BadRequestException
37
        ) {
38
            $response = $throwable->getResponse();
39
            $body = $response->getBody();
40
            if (!($body instanceof ParsedContentsInterface)) {
41
                return reject($throwable);
42
            }
43
            $contents = $body->getParsedContents();
44
            $message = $contents['message'] ?? $throwable->getMessage();
45
46
            if (isset($contents['errors'])) {
47
                return reject(
48
                    ApiErrorException::createWithErrors(
49
                        $message,
50
                        $throwable->getCode(),
51
                        $contents['errors'],
52
                        $throwable
53
                    )
54
                );
55
            }
56
57
            return reject(
58
                ApiErrorException::create(
59
                    $message,
60
                    $throwable->getCode(),
61
                    $throwable
62
                )
63
            );
64
        }
65
66
        return reject($throwable);
67
    }
68
}
69