GuzzleTransport   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
c 2
b 1
f 0
lcom 1
cbo 5
dl 0
loc 56
ccs 0
cts 32
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
A sendRequest() 0 18 2
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