Completed
Push — master ( 9a2704...923116 )
by Guillaume
02:03
created

ServiceApiCall::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
require __DIR__ . '/../vendor/autoload.php';
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
9
/**
10
 * ServiceApiCall
11
 */
12
class ServiceApiCall
13
{
14
    private $client = null;
15
    private $code = '';
16
    private $message = '';
17
    private $data = null;
18
19
    public function __construct(array $config = [])
20
    {
21
        $this->client = new Client($config);
22
    }
23
24
    public function send($method, $uri = '', array $options = [])
25
    {
26
        $res = true;
27
28
        try {
29
            $response = $this->client->request(strtoupper($method), $uri, $options);
30
            $this->code = $response->getStatusCode();
31
            $this->data = json_decode($response->getBody(), true);
32
        } catch (ConnectException $e) {
33
            $this->message = 'cannot connect to api!';
34
            $res = false;
35
        } catch (ClientException $e) {
36
            $this->message = $e->getResponse()->getBody();
37
        }
38
39
        return $res;
40
    }
41
42
    public function last($status = '')
43
    {
44
        $res = false;
45
46
        switch ($status) {
47
            case 'success':
48
                if ($this->code >= 200 || $this->code <= 299) {
49
                    $res = true;
50
                }
51
                break;
52
        }
53
54
        return $res;
55
    }
56
57
    public function getCode()
58
    {
59
        return $this->code;
60
    }
61
62
    public function getMessage()
63
    {
64
        return $this->message;
65
    }
66
67
    public function getData()
68
    {
69
        return $this->data;
70
    }
71
72
}
73