LokaliseClient::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 18
rs 9.9
1
<?php // Copyright ⓒ 2018 Magneds IP B.V. - All Rights Reserved
2
namespace Magneds\Lokalise\Client;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\ClientInterface;
6
use Magneds\Lokalise\RequestInterface;
7
use Magneds\Lokalise\ResponseInfo;
8
9
class LokaliseClient
10
{
11
    /**
12
     * @var ClientInterface
13
     */
14
    protected $client;
15
16
    /**
17
     * @var string
18
     */
19
    protected $apiKey;
20
21
    /**
22
     * LokaliseClient constructor.
23
     * @param ClientInterface $client
24
     * @param string $apiKey
25
     */
26
    public function __construct(string $apiKey, ClientInterface $client = null)
27
    {
28
        $this->apiKey = $apiKey;
29
30
        if (is_null($client)) {
31
            $client = new Client([
32
                'base_uri' => 'https://api.lokalise.co/api/',
33
            ]);
34
        }
35
36
        $this->client = $client;
37
    }
38
39
    /**
40
     * @param RequestInterface $request
41
     * @return ResponseInfo
42
     * @throws \GuzzleHttp\Exception\GuzzleException
43
     */
44
    public function send(RequestInterface $request)
45
    {
46
        $method = $request->getMethod();
47
        $uri    = $request->getURI();
48
49
        $options = [
50
            'query'       => ['api_token' => $this->apiKey],
51
            'form_params' => array_merge(['api_token' => $this->apiKey], $request->getBody()),
52
            'idn_conversion' => false
53
        ];
54
55
        $response = $this->client->request(
56
            $method,
57
            $uri,
58
            $options
59
        );
60
61
        return $request->handleResponse($response);
62
    }
63
}
64