Completed
Push — master ( c79f7e...463ede )
by Gallice
02:34
created

Client   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 98
ccs 22
cts 26
cp 0.8462
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A send() 0 16 4
A getLastResponse() 0 4 1
A getDefaultHeaders() 0 9 1
1
<?php
2
3
namespace Tgallice\Wit;
4
5
use GuzzleHttp\Client as HttpClient;
6
use GuzzleHttp\RequestOptions;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Log\LoggerInterface;
9
10
class Client
11
{
12
    /**
13
     * API base uri
14
     */
15
    const API_BASE_URI = 'https://api.wit.ai/';
16
17
    /*
18
     * API Version
19
     */
20
    const API_VERSION = '20160330';
21
22
    /**
23
     * Request default timeout
24
     */
25
    const DEFAULT_TIMEOUT = 5;
26
27
    /**
28
     * @var HTTP client
29
     */
30
    private $client;
31
32
    /**
33
     * @var LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @var ResponseInterface|null
39
     */
40
    private $lastResponse;
41
42
    /**
43
     * @var string Wit app token
44
     */
45
    private $accessToken;
46
47 5
    public function __construct($accessToken, $httpClient = null, LoggerInterface $logger = null)
48
    {
49 5
        $this->accessToken = $accessToken;
50 5
        $this->client = $httpClient ?: new HttpClient([
51
            'base_uri' => self::API_BASE_URI,
52
            'timeout' => self::DEFAULT_TIMEOUT,
53
            'connect_timeout' => self::DEFAULT_TIMEOUT,
54
        ]);
55 5
        $this->logger = $logger;
56 5
    }
57
58
    /**
59
     * @param string $method
60
     * @param string $uri
61
     * @param array $query
62
     * @param mixed $content
63
     *
64
     * @return ResponseInterface
65
     */
66 4
    public function send($method, $uri, array $query = [], $content = null)
67
    {
68
        $options = [
69 4
            RequestOptions::QUERY => $query,
70 4
            RequestOptions::HEADERS => $this->getDefaultHeaders()
71 4
        ];
72
73 4
        if (!empty($content)) {
74 1
            $type = is_array($content) || $content instanceof \JsonSerializable ? RequestOptions::JSON : RequestOptions::BODY;
75 1
            $options[$type] = $content;
76 1
        }
77
78 4
        $this->lastResponse = $this->client->request($method, $uri, $options);
79
80 4
        return $this->lastResponse;
81
    }
82
83
    /**
84
     * Get the last response from the API
85
     *
86
     * @return null|ResponseInterface
87
     */
88 1
    public function getLastResponse()
89
    {
90 1
        return $this->lastResponse;
91
    }
92
93
    /**
94
     * Get the defaults headers like the Authorization field
95
     *
96
     * @return array
97
     */
98 4
    protected function getDefaultHeaders()
99
    {
100
        return [
101 4
            'Authorization' => 'Bearer '.$this->accessToken,
102
            // Used the accept field is needed to fix the API version and avoid BC break from the API
103 4
            'Accept' => 'application/vnd.wit.'.self::API_VERSION.'+json',
104 4
            'Content-Type' => 'application/json',
105 4
        ];
106
    }
107
}
108