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 ( e7c613...bcbd1d )
by Cees-Jan
02:29
created

Oauth1Middleware   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A pre() 0 10 2
B validateOptions() 0 23 5
A signRequest() 0 10 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Middleware\Oauth1;
4
5
use ApiClients\Foundation\Middleware\DefaultPriorityTrait;
6
use ApiClients\Foundation\Middleware\MiddlewareInterface;
7
use ApiClients\Foundation\Middleware\PostTrait;
8
use ApiClients\Tools\Psr7\Oauth1\Definition;
9
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
10
use Psr\Http\Message\RequestInterface;
11
use React\EventLoop\LoopInterface;
12
use React\Promise\CancellablePromiseInterface;
13
use function GuzzleHttp\Psr7\parse_query;
14
use function React\Promise\resolve;
15
use function WyriHaximus\React\futurePromise;
16
17
class Oauth1Middleware implements MiddlewareInterface
18
{
19
    use DefaultPriorityTrait;
20
    use PostTrait;
21
22
    /**
23
     * @var LoopInterface
24
     */
25
    private $loop;
26
27
    /**
28
     * @param LoopInterface $loop
29
     */
30
    public function __construct(LoopInterface $loop)
31
    {
32
        $this->loop = $loop;
33
    }
34
35
    /**
36
     * @param RequestInterface $request
37
     * @param array $options
38
     * @return CancellablePromiseInterface
39
     */
40
    public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
41
    {
42
        if (!$this->validateOptions($options)) {
43
            return resolve($request);
44
        }
45
46
        return futurePromise($this->loop, [$request, $options])->then(function ($args) {
47
            return resolve($this->signRequest(...$args));
0 ignored issues
show
Bug introduced by
The call to signRequest() misses a required argument $options.

This check looks for function calls that miss required arguments.

Loading history...
48
        });
49
    }
50
51
    private function validateOptions(array $options): bool
52
    {
53
        if (!isset($options[self::class])) {
54
            return false;
55
        }
56
57
        foreach ([
58
            Options::CONSUMER_KEY,
59
            Options::CONSUMER_SECRET,
60
            Options::ACCESS_TOKEN,
61
            Options::TOKEN_SECRET,
62
        ] as $option) {
63
            if (!isset($options[self::class][$option])) {
64
                return false;
65
            }
66
67
            if (!($options[self::class][$option] instanceof $option)) {
68
                return false;
69
            }
70
        }
71
72
        return true;
73
    }
74
75
    private function signRequest(RequestInterface $request, array $options): RequestInterface
76
    {
77
        return (new RequestSigner(
78
            new Definition\ConsumerKey($options[self::class][Options::CONSUMER_KEY]),
79
            new Definition\ConsumerSecret($options[self::class][Options::CONSUMER_SECRET])
80
        ))->withAccessToken(
81
            new Definition\AccessToken($options[self::class][Options::ACCESS_TOKEN]),
82
            new Definition\TokenSecret($options[self::class][Options::TOKEN_SECRET])
83
        )->sign($request);
84
    }
85
}
86