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