Completed
Push — master ( 7668c7...bd1406 )
by Guillaume
02:28
created

ServiceApiCall::__construct()   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 1
1
<?php
2
3
namespace AlfredTime;
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
    /**
15
     * @var mixed
16
     */
17
    private $client = null;
18
19
    /**
20
     * @var string
21
     */
22
    private $code = '';
23
24
    /**
25
     * @var mixed
26
     */
27
    private $data = null;
28
29
    /**
30
     * @var string
31
     */
32
    private $message = '';
33
34
    /**
35
     * @param array $config
36
     */
37
    public function __construct(array $config = [])
38
    {
39
        $this->client = new Client($config);
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function getCode()
46
    {
47
        return $this->code;
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function getData()
54
    {
55
        return $this->data;
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61
    public function getMessage()
62
    {
63
        return $this->message;
64
    }
65
66
    /**
67
     * @param  $status
68
     * @return mixed
69
     */
70
    public function last($status = '')
71
    {
72
        $res = false;
73
74
        switch ($status) {
75
            case 'success':
76
                if ($this->code >= 200 || $this->code <= 299) {
77
                    $res = true;
78
                }
79
80
                break;
81
        }
82
83
        return $res;
84
    }
85
86
    /**
87
     * @param  $method
88
     * @param  $uri
89
     * @param  array     $options
90
     * @return mixed
91
     */
92
    public function send($method, $uri = '', array $options = [])
93
    {
94
        $res = true;
95
96
        try {
97
            $response = $this->client->request(strtoupper($method), $uri, $options);
98
            $this->code = $response->getStatusCode();
99
            $this->data = json_decode($response->getBody(), true);
100
        } catch (ConnectException $e) {
101
            $this->message = 'cannot connect to api!';
102
            $res = false;
103
        } catch (ClientException $e) {
104
            $this->message = $e->getResponse()->getBody();
105
        }
106
107
        return $res;
108
    }
109
}
110