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.

Client   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 6 1
A getHttpClient() 0 9 1
A getHandlerStack() 0 12 2
A containsAccessToken() 0 8 2
A __construct() 0 4 2
A get() 0 8 1
1
<?php
2
3
namespace Mozammil\Putio\Http;
4
5
use GuzzleHttp\Psr7\Uri;
6
use GuzzleHttp\Middleware;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Request;
9
use GuzzleHttp\Handler\CurlHandler;
10
use GuzzleHttp\Client as GuzzleClient;
11
use Psr\Http\Message\RequestInterface;
12
13
class Client
14
{
15
    /**
16
     * The API's base URI.
17
     */
18
    const URL = 'https://api.put.io/';
19
20
    /**
21
     * The API's version.
22
     */
23
    const VERSION = 'v2';
24
25
    /**
26
     * The HTTP client.
27
     *
28
     * @var \GuzzleHttp\Client
29
     */
30
    protected $http;
31
32
    /**
33
     * Construct the HTTP client and passing the
34
     * token for authorization.
35
     *
36
     * @param string $token
37
     */
38
    public function __construct(string $token)
39
    {
40
        $this->http = $this->http ?: $this->getHttpClient($token);
41
    }
42
43
    /**
44
     * Sends a GET Request.
45
     *
46
     * @param string $uri
47
     * @param array $query
48
     * @param array $options
49
     *
50
     * @return string JSON
51
     */
52
    public function get(string $uri, array $query = [], array $options = [])
53
    {
54
        $uri = $uri.'?'.http_build_query($query);
55
56
        $response = $this->http->request('GET', $uri, $options);
57
58
        return (string) $response->getBody()->getContents();
59
    }
60
61
    /**
62
     * Sends a POST Request.
63
     *
64
     * @param string $uri
65
     * @param array $options
66
     *
67
     * @return string JSON
68
     */
69
    public function post(string $uri, array $options = [])
70
    {
71
        $response = $this->http->request('POST', $uri, $options);
72
73
        return (string) $response->getBody()->getContents();
74
    }
75
76
    /**
77
     * Constructs the HTTP client.
78
     *
79
     * @param string $token
80
     *
81
     * @return \GuzzleHttp\Client
82
     */
83
    private function getHttpClient(string $token)
84
    {
85
        $stack = $this->getHandlerStack($token);
86
87
        return new GuzzleClient([
88
            'base_uri' => self::URL.self::VERSION.'/',
89
            'handler' => $stack,
90
        ]);
91
    }
92
93
    /**
94
     * Creates Guzzle Handler stack
95
     * Also appends the Oauth Token to every request.
96
     *
97
     * @param string $token
98
     *
99
     * @return \GuzzleHttp\HandlerStack $stack
100
     */
101
    private function getHandlerStack(string $token)
102
    {
103
        $stack = HandlerStack::create(new CurlHandler());
104
105
        $stack->push(Middleware::mapRequest(function (RequestInterface $request) use ($token) {
106
            return ! $this->containsAccessToken($request)
107
                ? $request->withUri(Uri::withQueryValue($request->getUri(), 'oauth_token', $token))
108
                : $request;
109
        }));
110
111
        return $stack;
112
    }
113
114
    /**
115
     * Checks if the request already contains an oauth_token.
116
     *
117
     * @param \Psr\Http\Message\RequestInterface $request
118
     *
119
     * @return bool
120
     */
121
    private function containsAccessToken(RequestInterface $request)
122
    {
123
        if (strpos($request->getUri()->getQuery(), 'oauth_token') !== false) {
124
            return true;
125
        }
126
127
        return false;
128
    }
129
}
130