GuzzleTransport::sendRequest()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 18
ccs 0
cts 16
cp 0
rs 9.4285
cc 2
eloc 10
nc 3
nop 1
crap 6
1
<?php
2
3
namespace Speicher210\Fastbill\Api\Transport;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\RequestException;
7
use Speicher210\Fastbill\Api\ApiCredentials;
8
9
/**
10
 * Transport implementation with Guzzle.
11
 */
12
class GuzzleTransport extends AbstractTransport
13
{
14
    /**
15
     * The Guzzle client.
16
     *
17
     * @var Client
18
     */
19
    protected $client;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param ApiCredentials $credentials The API credentials.
25
     */
26
    public function __construct(ApiCredentials $credentials)
27
    {
28
        parent::__construct($credentials);
29
30
        $this->client = new Client(
31
            [
32
                // Base URI is used with relative requests
33
                'base_uri' => self::BASE_URL,
34
                // You can set any number of default request options.
35
                'auth' => [
36
                    $this->apiCredentials->getEmail(),
37
                    $this->apiCredentials->getApiKey(),
38
                ],
39
                'headers' => array(
40
                    'Content-Type' => 'application/json',
41
                ),
42
            ]
43
        );
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function sendRequest($body)
50
    {
51
        try {
52
            $response = $this->client->request(
53
                'POST',
54
                null,
55
                [
56
                    'body' => $body
57
                ]
58
            );
59
60
            $body = $response->getBody()->getContents();
61
        } catch (RequestException $e) {
62
            $body = $e->getResponse()->getBody()->getContents();
63
        }
64
65
        return $body;
66
    }
67
}
68