Passed
Pull Request — master (#1)
by Samuel
03:41 queued 02:16
created

HttpClient   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 20
eloc 55
dl 0
loc 161
c 3
b 0
f 0
ccs 54
cts 60
cp 0.9
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getSender() 0 3 1
A send() 0 19 3
A setSender() 0 3 1
A prepareQueryString() 0 10 3
A prepareUrl() 0 3 1
A setLogger() 0 3 1
A getLogger() 0 3 1
A put() 0 3 1
A get() 0 3 1
A prepareOptions() 0 20 2
A post() 0 3 1
A delete() 0 3 1
A __construct() 0 12 1
A request() 0 13 2
1
<?php
2
3
namespace Elmage\TextNg;
4
5
use Elmage\TextNg\Authentication\ApiKeyAuthentication;
6
use Elmage\TextNg\Enum\Param;
7
use Elmage\TextNg\Exception\HttpException;
8
use GuzzleHttp\ClientInterface;
9
use GuzzleHttp\Exception\RequestException;
10
use GuzzleHttp\Psr7\Request;
11
use GuzzleHttp\RequestOptions;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Psr\Log\LoggerInterface;
15
16
class HttpClient
17
{
18
    private $apiUrl;
19
    private $apiVersion;
20
    private $auth;
21
    private $transport;
22
23
    /** @var string Text Message Sender */
24
    private $sender;
25
26
    /** @var LoggerInterface */
27
    private $logger;
28
29
    /** @var RequestInterface */
30
    private $lastRequest;
31
32
    /** @var ResponseInterface */
33
    private $lastResponse;
34
35 16
    public function __construct(
36
        string $apiUrl,
37
        string $apiVersion,
38
        ApiKeyAuthentication $auth,
39
        string $sender,
40
        ClientInterface $transport
41
    ) {
42 16
        $this->apiUrl = rtrim($apiUrl, '/');
43 16
        $this->apiVersion = $apiVersion;
44 16
        $this->auth = $auth;
45 16
        $this->sender = $sender;
46 16
        $this->transport = $transport;
47 16
    }
48
49
    public function getLogger()
50
    {
51
        return $this->logger;
52
    }
53
54 4
    public function setLogger(LoggerInterface $logger = null)
55
    {
56 4
        $this->logger = $logger;
57 4
    }
58
59 2
    public function getSender()
60
    {
61 2
        return $this->sender;
62
    }
63
64 2
    public function setSender(string $sender)
65
    {
66 2
        $this->sender = $sender;
67 2
    }
68
69
    /** @return ResponseInterface */
70 4
    public function get($path, array $params = array()): ResponseInterface
71
    {
72 4
        return $this->request('GET', $path, $params);
73
    }
74
75
    /** @return ResponseInterface */
76 4
    public function post($path, array $params = array()): ResponseInterface
77
    {
78 4
        return $this->request('POST', $path, $params);
79
    }
80
81
    /** @return ResponseInterface */
82 2
    public function put($path, array $params = array()): ResponseInterface
83
    {
84 2
        return $this->request('PUT', $path, $params);
85
    }
86
87
    /** @return ResponseInterface */
88 2
    public function delete($path, array $params = array()): ResponseInterface
89
    {
90 2
        return $this->request('DELETE', $path, $params);
91
    }
92
93
    /**
94
     * @param string $method http request method
95
     * @param string $path   url path
96
     * @param array  $params request parameters
97
     */
98 12
    private function request(string $method, string $path, array $params = array()): ResponseInterface
99
    {
100 12
        $method = strtoupper($method);
101
102 12
        $params[Param::API_KEY] = $this->auth->getApiKey();
103
104 12
        if ($method === 'GET') {
105 4
            $path = $this->prepareQueryString($path, $params);
106
        }
107
108 12
        $request = new Request($method, $this->prepareUrl($path));
109
110 12
        return $this->send($request, $params);
111
    }
112
113 12
    private function send(RequestInterface $request, array $params = array()): ResponseInterface
114
    {
115 12
        $this->lastRequest = $request;
116
117 12
        $options = $this->prepareOptions(
118 12
            $params
119
        );
120
121
        try {
122 12
            $this->lastResponse = $response = $this->transport->send($request, $options);
123
        } catch (RequestException $e) {
124
            throw HttpException::wrap($e);
125
        }
126
127 12
        if ($this->logger) {
128
            //$this->logWarnings($response); TODO LOG warnings
129
        }
130
131 12
        return $response;
132
    }
133
134
    /**
135
     * @param $path //request path
0 ignored issues
show
Documentation Bug introduced by
The doc comment //request at position 0 could not be parsed: Unknown type name '//request' at position 0 in //request.
Loading history...
136
     * @param array $params pointer to an array of parameters
137
     *
138
     * @return string
139
     */
140 4
    private function prepareQueryString(string $path, array &$params = array()): string
141
    {
142 4
        if (count($params) < 1) {
143
            return $path;
144
        }
145
146 4
        $path .= false === strpos($path, '?') ? '?' : '&';
147 4
        $path .= http_build_query($params, '', '&');
148
149 4
        return $path;
150
    }
151
152 12
    private function prepareOptions(array $params = array())
153
    {
154 12
        $options = array();
155
156 12
        if (count($params) > 0) {
157 12
            $options[RequestOptions::JSON] = $params;
158 12
            $body = json_encode($params);
0 ignored issues
show
Unused Code introduced by
The assignment to $body is dead and can be removed.
Loading history...
159
        } else {
160
            $body = '';
161
        }
162
163
        $defaultHeaders = array(
164 12
            'User-Agent' => 'elmage/textng/php/'.Client::VERSION,
165 12
            'Content-Type' => 'application/json',
166 12
            'Accept' => 'text/plain, application/json',
167
        );
168
169 12
        $options[RequestOptions::HEADERS] = $defaultHeaders;
170
171 12
        return $options;
172
    }
173
174 12
    private function prepareUrl(string $path): string
175
    {
176 12
        return $this->apiUrl.'/'.ltrim($path, '/');
177
    }
178
}
179