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
Pull Request — master (#9)
by Gallice
03:41
created

Client   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 4
dl 0
loc 180
ccs 45
cts 45
cp 1
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A post() 0 4 1
A get() 0 4 1
A put() 0 4 1
A delete() 0 4 1
A send() 0 18 2
A getLastResponse() 0 4 1
A getHttpClient() 0 4 1
A addToken() 0 6 1
A validateResponse() 0 10 4
A defaultHttpClient() 0 8 1
A defaultHeaders() 0 6 1
1
<?php
2
3
namespace Tgallice\FBMessenger;
4
5
use GuzzleHttp\ClientInterface;
6
use GuzzleHttp\Exception\GuzzleException;
7
use GuzzleHttp\RequestOptions;
8
use Psr\Http\Message\ResponseInterface;
9
use Tgallice\FBMessenger\Exception\ApiException;
10
11
class Client
12
{
13
    /**
14
     * API base uri
15
     */
16
    const API_BASE_URI = 'https://graph.facebook.com/';
17
18
    /*
19
     * API Version
20
     */
21
    const DEFAULT_API_VERSION = 'v2.6';
22
23
    /**
24
     * Request default timeout
25
     */
26
    const DEFAULT_TIMEOUT = 60;
27
28
    /**
29
     * Request default file upload timeout
30
     */
31
    const DEFAULT_FILE_UPLOAD_TIMEOUT = 3600;
32
33
    /**
34
     * @var array
35
     */
36
    public static $allowedMethod = ['POST', 'GET', 'PUT', 'DELETE'];
37
38
    /**
39
     * @var string Wit app token
40
     */
41
    private $accessToken;
42
43
    /**
44
     * @var ClientInterface client
45
     */
46
    private $client;
47
48
    /**
49
     * @var ResponseInterface|null
50
     */
51
    private $lastResponse;
52
53 14
    public function __construct($accessToken, ClientInterface $httpClient = null)
54
    {
55 14
        $this->accessToken = $accessToken;
56 14
        $this->client = $httpClient ?: $this->defaultHttpClient();
57 14
    }
58
59
    /**
60
     * @param string $uri
61
     * @param string|null $body
62
     *
63
     * @return ResponseInterface
64
     */
65 1
    public function post($uri, $body = null)
66
    {
67 1
        return $this->send('POST', $uri, $body);
68
    }
69
70
    /**
71
     * @param string $uri
72
     * @param array $params
73
     *
74
     * @return ResponseInterface
75
     */
76 1
    public function get($uri, array $params = [])
77
    {
78 1
        return $this->send('GET', $uri, null, $params);
79
    }
80
81
    /**
82
     * @param string $uri
83
     * @param null|string $data
84
     *
85
     * @return ResponseInterface
86
     */
87 1
    public function put($uri, $data = null)
88
    {
89 1
        return $this->send('PUT', $uri, $data);
90
    }
91
92
    /**
93
     * @param string $uri
94
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
     *
96
     * @return ResponseInterface
97
     */
98 1
    public function delete($uri)
99
    {
100 1
        return $this->send('DELETE', $uri);
101
    }
102
103
    /**
104
     * @param string $method
105
     * @param string $uri
106
     * @param mixed $body
107
     * @param array $query
108
     * @param array $headers
109
     * @param array $options
110
     *
111
     * @return ResponseInterface
112
     *
113
     * @throws ApiException
114
     */
115 12
    public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
116
    {
117 12
        $query = $this->addToken($query);
118
119
        try {
120 12
            $options[RequestOptions::BODY] = $body;
121 12
            $options[RequestOptions::QUERY] = $query;
122 12
            $options[RequestOptions::HEADERS] = array_merge($this->defaultHeaders(), $headers);
123
124 12
            $this->lastResponse = $this->client->request($method, $uri, $options);
125 12
        } catch (GuzzleException $e) {
126 1
            throw new ApiException($e->getMessage(), $e->getCode());
127
        }
128
129 11
        $this->validateResponse($this->lastResponse);
130
131 10
        return $this->lastResponse;
132
    }
133
134
    /**
135
     * Get the last response from the API
136
     *
137
     * @return null|ResponseInterface
138
     */
139 1
    public function getLastResponse()
140
    {
141 1
        return $this->lastResponse;
142
    }
143
144 1
    public function getHttpClient()
145
    {
146 1
        return $this->client;
147
    }
148
149 12
    private function addToken(array $query)
150
    {
151 12
        $query['access_token'] = $this->accessToken;
152
153 12
        return $query;
154
    }
155
156
    /**
157
     * @param ResponseInterface $response
158
     *
159
     * @throws ApiException
160
     */
161 11
    private function validateResponse(ResponseInterface $response)
162
    {
163 11
        if ($response->getStatusCode() !== 200) {
164 1
            $responseData = json_decode((string) $response->getBody(), true);
165 1
            $code = isset($responseData['error']['code']) ? $responseData['error']['code'] : 0;
166 1
            $message = isset($responseData['error']['message']) ? $responseData['error']['message'] : $response->getReasonPhrase();
167
168 1
            throw new ApiException($message, $code, $responseData);
169
        }
170 10
    }
171
172
    /**
173
     * @return ClientInterface
174
     */
175 1
    private function defaultHttpClient()
176
    {
177 1
        return new \GuzzleHttp\Client([
178 1
            'base_uri' => self::API_BASE_URI . self::DEFAULT_API_VERSION,
179 1
            'timeout' => self::DEFAULT_TIMEOUT,
180 1
            'connect_timeout' => 10,
181 1
        ]);
182
    }
183
184 12
    private function defaultHeaders()
185
    {
186
        return [
187 12
            'Content-Type' => 'application/json',
188 12
        ];
189
    }
190
}
191