Client::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace ChrisArmitage\ScalewayApi;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
7
class Client
8
{
9
    protected $guzzle;
10
    protected $endpoint;
11
    protected $token;
12
    protected $resource;
13
    protected $method;
14
    protected $parameters = [];
15
16 4
    public function __construct(GuzzleClient $guzzle, $endpoint, $token) {
17 4
        $this->guzzle = $guzzle;
18 4
        $this->endpoint = $endpoint;
19 4
        $this->token = $token;
20 4
    }
21
22 3
    public function setResource($resource) {
23 3
        $this->resource = $resource;
24 3
        return $this;
25
    }
26
27 3
    public function setMethod($method) {
28 3
        $this->method = $method;
29 3
        return $this;
30
    }
31
32 1
    public function setParameters($parameters) {
33 1
        $this->parameters = $parameters;
34 1
        return $this;
35
    }
36
37 4
    public function call() {
38
        $options = [
39
            'headers' => [
40 4
                'X-Auth-Token' => $this->token,
41 4
            ],
42 4
        ];
43
44 4
        switch ($this->method) {
45 4
            case 'GET':
46 1
                $response = $this->guzzle->get("{$this->endpoint}{$this->resource}", $options);
47 1
                break;
48 3
            case 'POST':
49 1
                $options['json'] = $this->parameters;
50 1
                $response = $this->guzzle->post("{$this->endpoint}{$this->resource}", $options);
51 1
                break;
52 2
            case 'DELETE':
53 1
                $response = $this->guzzle->delete("{$this->endpoint}{$this->resource}", $options);
54 1
                break;
55 1
            default:
56 1
                throw new \RuntimeException('No valid method set');
57 4
        }
58
59 3
        return $response->getBody()->getContents();
60
    }
61
}
62