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 ( a2c9ae...43a136 )
by Cees-Jan
10:45 queued 14s
created

Oauth1Middleware   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 75
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 24 6
A signRequest() 0 16 1
1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Oauth1\Middleware;
4
5
use ApiClients\Foundation\Middleware\MiddlewareInterface;
6
use ApiClients\Foundation\Middleware\PostTrait;
7
use ApiClients\Foundation\Oauth1\Options;
8
use ApiClients\Tools\Psr7\Oauth1\Definition;
9
use ApiClients\Tools\Psr7\Oauth1\RequestSigning\RequestSigner;
10
use JacobKiers\OAuth\Consumer\ConsumerInterface;
11
use JacobKiers\OAuth\SignatureMethod\SignatureMethodInterface;
12
use JacobKiers\OAuth\Token\TokenInterface;
13
use Psr\Http\Message\RequestInterface;
14
use React\EventLoop\LoopInterface;
15
use React\Promise\CancellablePromiseInterface;
16
use function React\Promise\resolve;
17
use function GuzzleHttp\Psr7\parse_query;
18
use function WyriHaximus\React\futurePromise;
19
20
class Oauth1Middleware implements MiddlewareInterface
21
{
22
    use PostTrait;
23
24
    /**
25
     * @var LoopInterface
26
     */
27
    private $loop;
28
29
    /**
30
     * @param LoopInterface $loop
31
     */
32
    public function __construct(LoopInterface $loop)
33
    {
34
        $this->loop = $loop;
35
    }
36
37
    /**
38
     * @param RequestInterface $request
39
     * @param array $options
40
     * @return CancellablePromiseInterface
41
     */
42
    public function pre(RequestInterface $request, array $options = []): CancellablePromiseInterface
43
    {
44
        if (!$this->validateOptions($options)) {
45
            return resolve($request);
46
        }
47
48
        return futurePromise($this->loop, [$request, $options])->then(function ($args) {
49
            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...
50
        });
51
    }
52
53
    private function validateOptions(array $options): bool
54
    {
55
        if (!isset($options[self::class])) {
56
            return false;
57
        }
58
59
        if (!isset($options[self::class][Options::CONSUMER])) {
60
            return false;
61
        }
62
63
        if (!($options[self::class][Options::CONSUMER] instanceof ConsumerInterface)) {
64
            return false;
65
        }
66
67
        if (!isset($options[self::class][Options::TOKEN])) {
68
            return false;
69
        }
70
71
        if (!($options[self::class][Options::TOKEN] instanceof TokenInterface)) {
72
            return false;
73
        }
74
75
        return true;
76
    }
77
78
    private function signRequest(RequestInterface $request, array $options): RequestInterface
79
    {
80
        /** @var ConsumerInterface */
81
        $consumer = $options[self::class][Options::CONSUMER];
82
83
        /** @var TokenInterface */
84
        $token = $options[self::class][Options::TOKEN];
85
86
        return (new RequestSigner(
87
            new Definition\ConsumerKey($consumer->getKey()),
88
            new Definition\ConsumerSecret($consumer->getSecret())
89
        ))->withAccessToken(
90
            new Definition\AccessToken($token->getKey()),
91
            new Definition\TokenSecret($token->getSecret())
92
        )->sign($request);
93
    }
94
}
95