Completed
Push — master ( 593fa0...c02250 )
by Gallice
02:59
created

Client::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Tgallice\Wit;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\RequestOptions;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Log\LoggerInterface;
10
use Tgallice\Wit\Exception\BadResponseException;
11
12
class Client
13
{
14
    /**
15
     * API base uri
16
     */
17
    const API_BASE_URI = 'https://api.wit.ai/';
18
19
    /*
20
     * API Version
21
     */
22
    const API_VERSION = '20160330';
23
24
    /**
25
     * Request default timeout
26
     */
27
    const DEFAULT_TIMEOUT = 5;
28
29
    /**
30
     * @var ClientInterface client
31
     */
32
    private $client;
33
34
    /**
35
     * @var LoggerInterface
36
     */
37
    private $logger;
38
39
    /**
40
     * @var ResponseInterface|null
41
     */
42
    private $lastResponse;
43
44
    /**
45
     * @var string Wit app token
46
     */
47
    private $accessToken;
48
49 11
    public function __construct($accessToken, ClientInterface $httpClient = null, LoggerInterface $logger = null)
50
    {
51 11
        $this->accessToken = $accessToken;
52 11
        $this->client = $httpClient ?: new HttpClient([
53 1
            'base_uri' => self::API_BASE_URI,
54 1
            'timeout' => self::DEFAULT_TIMEOUT,
55 1
            'connect_timeout' => self::DEFAULT_TIMEOUT,
56 1
        ]);
57 11
        $this->logger = $logger;
58 11
    }
59
60
    /**
61
     * @param $uri
62
     * @param array $params
63
     *
64
     * @return ResponseInterface
65
     */
66 1
    public function post($uri, array $params = [])
67
    {
68 1
        return $this->send('POST', $uri, [], $params);
69
    }
70
71
    /**
72
     * @param $uri
73
     * @param array $params
74
     *
75
     * @return ResponseInterface
76
     */
77 1
    public function get($uri, array $params = [])
78
    {
79 1
        return $this->send('GET', $uri, $params);
80
    }
81
82
    /**
83
     * @param $uri
84
     * @param array $params
85
     *
86
     * @return ResponseInterface
87
     */
88 1
    public function put($uri, array $params = [])
89
    {
90 1
        return $this->send('PUT', $uri, [], $params);
91
    }
92
93
    /**
94
     * @param $uri
95
     * @param array $params
0 ignored issues
show
Bug introduced by
There is no parameter named $params. 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...
96
     *
97
     * @return ResponseInterface
98
     */
99 1
    public function delete($uri)
100
    {
101 1
        return $this->send('DELETE', $uri);
102
    }
103
104
    /**
105
     * @param string $method
106
     * @param string $uri
107
     * @param array $queryParams
108
     * @param mixed $postParams
109
     *
110
     * @return ResponseInterface
111
     */
112 9
    public function send($method, $uri, array $queryParams = [], $postParams = null)
113
    {
114
        $options = [
115 9
            RequestOptions::QUERY => $queryParams,
116 9
            RequestOptions::HEADERS => $this->getDefaultHeaders()
117 9
        ];
118
119 9
        if (!empty($postParams)) {
120 3
            $type = is_array($postParams) ? RequestOptions::JSON : RequestOptions::BODY;
121 3
            $options[$type] = $postParams;
122 3
        }
123
124 9
        $this->lastResponse = $this->client->request($method, $uri, $options);
125
126 9
        $this->validateResponse($this->lastResponse);
127
128 8
        return $this->lastResponse;
129
    }
130
131
    /**
132
     * Get the last response from the API
133
     *
134
     * @return null|ResponseInterface
135
     */
136 1
    public function getLastResponse()
137
    {
138 1
        return $this->lastResponse;
139
    }
140
141 1
    public function getHttpClient()
142
    {
143 1
        return $this->client;
144
    }
145
146
    /**
147
     * Get the defaults headers like the Authorization field
148
     *
149
     * @return array
150
     */
151 9
    private function getDefaultHeaders()
152
    {
153
        return [
154 9
            'Authorization' => 'Bearer '.$this->accessToken,
155
            // Used the accept field is needed to fix the API version and avoid BC break from the API
156 9
            'Accept' => 'application/vnd.wit.'.self::API_VERSION.'+json',
157 9
        ];
158
    }
159
160
    /**
161
     * @param ResponseInterface $response
162
     *
163
     * @return bool
164
     *
165
     * @throws BadResponseException
166
     */
167 9
    private function validateResponse(ResponseInterface $response)
168
    {
169 9
        if ($response->getStatusCode() !== 200) {
170 1
            $message = empty($response->getReasonPhrase()) ? 'Bad response status code' : $response->getReasonPhrase();
171
172 1
            throw new BadResponseException($message, $response);
173
        }
174
175 8
        return true;
176
    }
177
}
178