1 | <?php |
||
2 | /** |
||
3 | * Copyright (c) 2019 - present |
||
4 | * updown - UpDown.php |
||
5 | * author: Roberto Belotti - [email protected] |
||
6 | * web : robertobelotti.com, github.com/biscolab |
||
7 | * Initial version created on: 15/2/2019 |
||
8 | * MIT license: https://github.com/biscolab/updown-php/blob/master/LICENSE |
||
9 | */ |
||
10 | |||
11 | namespace Biscolab\UpDown; |
||
12 | |||
13 | use Biscolab\UpDown\Fields\UpDownRequestFields; |
||
14 | use Biscolab\UpDown\Http\UpDownClient; |
||
15 | use Biscolab\UpDown\Http\UpDownRequest; |
||
16 | use Biscolab\UpDown\Http\UpDownResponse; |
||
17 | |||
18 | /** |
||
19 | * Class UpDown |
||
20 | * @package Biscolab\UpDown\Abstracts |
||
21 | */ |
||
22 | class UpDown |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | const API_URL = "https://updown.io/api/"; |
||
29 | |||
30 | /** |
||
31 | * UpDown |
||
32 | * |
||
33 | * @var UpDown |
||
34 | */ |
||
35 | protected static $instance; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $result = ''; |
||
41 | |||
42 | /** |
||
43 | * @var Api |
||
44 | */ |
||
45 | protected $updown_api; |
||
46 | |||
47 | /** |
||
48 | * @var null|UpDownClient |
||
49 | */ |
||
50 | protected $client = null; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $key = ''; |
||
56 | |||
57 | /** |
||
58 | * Api constructor. |
||
59 | * |
||
60 | * @param array $config |
||
61 | */ |
||
62 | public function __construct(array $config = []) |
||
63 | { |
||
64 | |||
65 | // Set "API key" |
||
66 | $key = (empty($config[UpDownRequestFields::API_KEY])) ? '' : $config[UpDownRequestFields::API_KEY]; |
||
67 | $this->setKey($key); |
||
68 | |||
69 | $this->setClient(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $config |
||
74 | * |
||
75 | * @return UpDown |
||
76 | */ |
||
77 | public static function init($config): UpDown |
||
78 | { |
||
79 | |||
80 | $updown = new static($config); |
||
81 | static::setInstance($updown); |
||
82 | |||
83 | return $updown; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param UpDown $instance |
||
88 | */ |
||
89 | public static function setInstance(UpDown $instance) |
||
90 | { |
||
91 | |||
92 | static::$instance = $instance; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return UpDown |
||
97 | */ |
||
98 | public static function instance(): UpDown |
||
99 | { |
||
100 | |||
101 | return static::$instance; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | public function getKey(): string |
||
108 | { |
||
109 | |||
110 | return $this->key; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $key |
||
115 | * |
||
116 | * @return UpDown |
||
117 | */ |
||
118 | public function setKey(string $key): UpDown |
||
119 | { |
||
120 | |||
121 | $this->key = $key; |
||
122 | |||
123 | return $this; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return UpDownClient |
||
128 | */ |
||
129 | public function getClient(): UpDownClient |
||
130 | { |
||
131 | |||
132 | return $this->client; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param UpDownClient|null $client |
||
137 | * |
||
138 | * @return UpDown |
||
139 | */ |
||
140 | public function setClient(?UpDownClient $client = null): UpDown |
||
141 | { |
||
142 | |||
143 | $this->client = $client ?? new UpDownClient(); |
||
144 | |||
145 | return $this; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public function getServiceEndpoint(): string |
||
152 | { |
||
153 | |||
154 | return $this->updown_api->getServiceEndpoint(); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @param array $params |
||
159 | * |
||
160 | * @return mixed |
||
161 | */ |
||
162 | public function createRequest(array $params): UpDownRequest |
||
163 | { |
||
164 | |||
165 | return new UpDownRequest($params); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param string $path |
||
170 | * @param array $params |
||
171 | * |
||
172 | * @return mixed |
||
173 | */ |
||
174 | public function get(string $path, $params = []): UpDownResponse |
||
175 | { |
||
176 | |||
177 | return $this->client->get($path, $params); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param string $path |
||
182 | * @param array $params |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function post(string $path, $params = []): UpDownResponse |
||
187 | { |
||
188 | |||
189 | return $this->client->post($path, $params); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param string $path |
||
194 | * @param array $params |
||
195 | * |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function put(string $path, $params = []): UpDownResponse |
||
199 | { |
||
200 | |||
201 | return $this->client->put($path, $params); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param string $path |
||
206 | * @param array $params |
||
207 | * |
||
208 | * @return mixed |
||
209 | */ |
||
210 | public function delete(string $path, $params = []): UpDownResponse |
||
211 | { |
||
212 | |||
213 | return $this->client->delete($path, $params); |
||
214 | } |
||
215 | |||
216 | } |