1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat; |
4
|
|
|
|
5
|
|
|
use Lifeboat\Exceptions\BadMethodException; |
6
|
|
|
use Lifeboat\Exceptions\OAuthException; |
7
|
|
|
use Lifeboat\Models\Model; |
8
|
|
|
use Lifeboat\Services\ObjectFactory; |
9
|
|
|
use Lifeboat\Utils\Curl; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Connector |
13
|
|
|
* @package Lifeboat |
14
|
|
|
* |
15
|
|
|
* @property string $_auth_domain |
16
|
|
|
* @property string|null $_access_token |
17
|
|
|
* @property string $_site_key |
18
|
|
|
* @property Model $address |
19
|
|
|
*/ |
20
|
|
|
abstract class Connector { |
21
|
|
|
|
22
|
|
|
const AUTH_DOMAIN = 'https://accounts.lifeboat.app'; |
23
|
|
|
const TOKEN_URL = '/oauth/token'; |
24
|
|
|
const SITES_URL = '/oauth/sites'; |
25
|
|
|
|
26
|
|
|
protected string $_auth_domain = 'https://accounts.lifeboat.app'; |
27
|
|
|
protected string $_access_token; |
28
|
|
|
protected string $_site_key; |
29
|
|
|
protected string $_host; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
* |
34
|
|
|
* @throws OAuthException |
35
|
|
|
*/ |
36
|
|
|
abstract public function getAccessToken(): string; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $service |
40
|
|
|
* @return Model |
41
|
|
|
* @throws BadMethodException |
42
|
|
|
*/ |
43
|
|
|
public function __get(string $service): Model |
44
|
|
|
{ |
45
|
|
|
$obj = ObjectFactory::create($this, $service); |
46
|
|
|
if (!$obj) throw new BadMethodException("Service for `{$service}` does not exist"); |
47
|
|
|
|
48
|
|
|
return $obj; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
* @throws OAuthException |
54
|
|
|
*/ |
55
|
|
|
public function getSites(): array |
56
|
|
|
{ |
57
|
|
|
$curl = new Curl($this->auth_url(self::SITES_URL), [ |
58
|
|
|
'access_token' => $this->getAccessToken() |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$curl->setMethod('POST'); |
62
|
|
|
$response = $curl->curl(); |
63
|
|
|
|
64
|
|
|
if (!$response->isValid()) { |
65
|
|
|
throw new OAuthException($response->getRaw()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $response->getJSON(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Makes a request to the API to refresh the current access token |
73
|
|
|
* @see Client::getAccessToken() |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
* @throws OAuthException |
77
|
|
|
*/ |
78
|
|
|
public function refreshAccessToken(): Connector |
79
|
|
|
{ |
80
|
|
|
$this->_access_token = null; |
81
|
|
|
$this->getAccessToken(); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $host |
87
|
|
|
* @param string $site_key |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setActiveSite(string $host, string $site_key): Connector |
91
|
|
|
{ |
92
|
|
|
$this->_host = $host; |
93
|
|
|
$this->_site_key = $site_key; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $url |
100
|
|
|
* @param string $method |
101
|
|
|
* @param array $data |
102
|
|
|
* @param array $headers |
103
|
|
|
* @return CurlResponse |
104
|
|
|
* @throws OAuthException |
105
|
|
|
*/ |
106
|
|
|
public function curl_api(string $url, string $method = 'GET', array $data = [], array $headers = []): CurlResponse |
107
|
|
|
{ |
108
|
|
|
$curl = new Curl($url, $data, $headers); |
109
|
|
|
|
110
|
|
|
$curl->setMethod($method); |
111
|
|
|
$curl->addHeader('access-token', $this->getAccessToken()); |
112
|
|
|
$curl->addHeader('site-key', $this->_site_key); |
113
|
|
|
$curl->addHeader('Host', $this->_host); |
114
|
|
|
$curl->addHeader('Accept', 'application/json'); |
115
|
|
|
|
116
|
|
|
return $curl->curl_json(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $path |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
protected function auth_url(string $path): string |
124
|
|
|
{ |
125
|
|
|
return $this->_auth_domain . '/' . ltrim($path, '/'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|