1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\Services; |
4
|
|
|
|
5
|
|
|
use Lifeboat\Connector; |
6
|
|
|
use Lifeboat\Exceptions\ApiException; |
7
|
|
|
use Lifeboat\Exceptions\OAuthException; |
8
|
|
|
use Lifeboat\Factory\ObjectFactory; |
9
|
|
|
use Lifeboat\Resource\ApiResource; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ApiService |
13
|
|
|
* @package Lifeboat\Services |
14
|
|
|
* |
15
|
|
|
* @property Connector $client |
16
|
|
|
*/ |
17
|
|
|
abstract class ApiService { |
18
|
|
|
|
19
|
|
|
/** @var Connector $client */ |
20
|
|
|
protected $client; |
21
|
|
|
|
22
|
|
|
public function __construct(Connector $client) |
23
|
|
|
{ |
24
|
|
|
$this->setClient($client); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return Connector |
29
|
|
|
*/ |
30
|
|
|
public function getClient(): Connector |
31
|
|
|
{ |
32
|
|
|
return $this->client; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Connector $client |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setClient(Connector $client): ApiService |
40
|
|
|
{ |
41
|
|
|
$this->client = $client; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @see ObjectFactory::make() |
47
|
|
|
* @see Connector::curl_api() |
48
|
|
|
* |
49
|
|
|
* @param string $url |
50
|
|
|
* @param array $params |
51
|
|
|
* @return ApiResource|null |
52
|
|
|
* @throws ApiException |
53
|
|
|
* @throws OAuthException |
54
|
|
|
*/ |
55
|
|
|
protected function _get(string $url, array $params = []): ?ApiResource |
56
|
|
|
{ |
57
|
|
|
$curl = $this->getClient()->curl_api($url, 'GET', $params); |
58
|
|
|
|
59
|
|
|
if ($curl->isValid() && $curl->isJSON()) { |
60
|
|
|
return ObjectFactory::make($this->getClient(), $curl->getJSON()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new ApiException($curl->getError()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $url |
68
|
|
|
* @param array $params |
69
|
|
|
* @return ApiResource|bool|null |
70
|
|
|
* @throws ApiException |
71
|
|
|
* @throws OAuthException |
72
|
|
|
*/ |
73
|
|
|
protected function _post(string $url, array $params = []) |
74
|
|
|
{ |
75
|
|
|
$curl = $this->getClient()->curl_api($url, 'POST', $params); |
76
|
|
|
|
77
|
|
|
if ($curl->isValid()) { |
78
|
|
|
if ($curl->isJSON()) { |
79
|
|
|
return ObjectFactory::make($this->getClient(), $curl->getJSON()); |
80
|
|
|
} else { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
throw new ApiException($curl->getError()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $url |
90
|
|
|
* @param array $params |
91
|
|
|
* @return bool |
92
|
|
|
* @throws ApiException |
93
|
|
|
* @throws OAuthException |
94
|
|
|
*/ |
95
|
|
|
protected function _delete(string $url, array $params = []): bool |
96
|
|
|
{ |
97
|
|
|
$curl = $this->getClient()->curl_api($url, 'DELETE', $params); |
98
|
|
|
|
99
|
|
|
if ($curl->isValid()) return true; |
100
|
|
|
|
101
|
|
|
throw new ApiException($curl->getError()); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|