1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat; |
4
|
|
|
|
5
|
|
|
require_once 'includes/functions.php'; |
6
|
|
|
|
7
|
|
|
use Lifeboat\Exceptions\BadMethodException; |
8
|
|
|
use Lifeboat\Exceptions\OAuthException; |
9
|
|
|
use Lifeboat\Factory\ServiceFactory; |
10
|
|
|
use Lifeboat\Services\ApiService; |
11
|
|
|
use Lifeboat\Utils\Curl; |
12
|
|
|
use Lifeboat\Utils\URL; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Connector |
16
|
|
|
* @package Lifeboat |
17
|
|
|
* |
18
|
|
|
* @property string $_auth_domain |
19
|
|
|
* @property string|null $_access_token |
20
|
|
|
* @property string $_site_key |
21
|
|
|
* |
22
|
|
|
* // Services |
23
|
|
|
* @property \Lifeboat\Services\Orders $orders |
24
|
|
|
* @property \Lifeboat\Services\Addresses $addresses |
25
|
|
|
* @property \Lifeboat\Services\Customers $customers |
26
|
|
|
* @property \Lifeboat\Services\Collections $collections |
27
|
|
|
* @property \Lifeboat\Services\Pages $pages |
28
|
|
|
* @property \Lifeboat\Services\CustomPages $custom_pages |
29
|
|
|
* @property \Lifeboat\Services\DeliveryZones $delivery_zones |
30
|
|
|
*/ |
31
|
|
|
abstract class Connector { |
32
|
|
|
|
33
|
|
|
const AUTH_DOMAIN = 'https://accounts.lifeboat.app'; |
34
|
|
|
const SITES_URL = '/oauth/sites'; |
35
|
|
|
|
36
|
|
|
protected string $_auth_domain = 'https://accounts.lifeboat.app'; |
37
|
|
|
protected string $_access_token = ''; |
38
|
|
|
protected string $_site_key = ''; |
39
|
|
|
protected string $_host = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
* |
44
|
|
|
* @throws OAuthException |
45
|
|
|
*/ |
46
|
|
|
abstract public function getAccessToken(): string; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $service |
50
|
|
|
* @return ApiService|null |
51
|
|
|
* @throws BadMethodException |
52
|
|
|
*/ |
53
|
|
|
public function __get(string $service): ?ApiService |
54
|
|
|
{ |
55
|
|
|
$obj = ServiceFactory::inst($this, $service); |
56
|
|
|
if (!$obj) throw new BadMethodException("Service for `{$service}` does not exist"); |
57
|
|
|
|
58
|
|
|
return $obj; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
* @throws OAuthException |
64
|
|
|
*/ |
65
|
|
|
public function getSites(): array |
66
|
|
|
{ |
67
|
|
|
$curl = new Curl($this->auth_url(self::SITES_URL), [ |
68
|
|
|
'access_token' => $this->getAccessToken() |
69
|
|
|
]); |
70
|
|
|
|
71
|
|
|
$curl->setMethod('POST'); |
72
|
|
|
$response = $curl->curl(); |
73
|
|
|
|
74
|
|
|
if (!$response->isValid()) { |
75
|
|
|
$error = $response->getJSON(); |
76
|
|
|
throw new OAuthException($error['error'], $error['code']); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $response->getJSON() ?? []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Makes a request to the API to refresh the current access token |
84
|
|
|
* @see Client::getAccessToken() |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
* @throws OAuthException |
88
|
|
|
*/ |
89
|
|
|
public function refreshAccessToken(): Connector |
90
|
|
|
{ |
91
|
|
|
$this->_access_token = null; |
92
|
|
|
$this->getAccessToken(); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $host |
98
|
|
|
* @param string $site_key |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setActiveSite(string $host, string $site_key): Connector |
102
|
|
|
{ |
103
|
|
|
$this->_host = $host; |
104
|
|
|
$this->_site_key = $site_key; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getHost(): string |
113
|
|
|
{ |
114
|
|
|
return $this->_host; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getSiteKey(): string |
121
|
|
|
{ |
122
|
|
|
return $this->_site_key; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $url |
127
|
|
|
* @param string $method |
128
|
|
|
* @param array $data |
129
|
|
|
* @param array $headers |
130
|
|
|
* @return CurlResponse |
131
|
|
|
* @throws OAuthException |
132
|
|
|
*/ |
133
|
|
|
public function curl_api(string $url, string $method = 'GET', array $data = [], array $headers = []): CurlResponse |
134
|
|
|
{ |
135
|
|
|
$url = URL::is_absolute_url($url) ? $url |
136
|
|
|
: 'https://' . rtrim($this->getHost(), '/') . '/' . ltrim($url, '/'); |
137
|
|
|
|
138
|
|
|
$curl = new Curl($url, $data, $headers); |
139
|
|
|
|
140
|
|
|
$curl->setMethod($method); |
141
|
|
|
$curl->addHeader('access-token', $this->getAccessToken()); |
142
|
|
|
$curl->addHeader('site-key', $this->getSiteKey()); |
143
|
|
|
$curl->addHeader('Host', $this->getHost()); |
144
|
|
|
$curl->addHeader('Accept', 'application/json'); |
145
|
|
|
|
146
|
|
|
return $curl->curl_json(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $path |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function auth_url(string $path): string |
154
|
|
|
{ |
155
|
|
|
return rtrim($this->_auth_domain, '/') . '/' . ltrim($path, '/'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|