1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat; |
4
|
|
|
|
5
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . '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
|
|
|
* @property \Lifeboat\Services\TaxCodes $tax_codes |
31
|
|
|
* @property \Lifeboat\Services\Locations $locations |
32
|
|
|
* @property \Lifeboat\Services\Media $media |
33
|
|
|
* @property \Lifeboat\Services\Products $products |
34
|
|
|
*/ |
35
|
|
|
abstract class Connector { |
36
|
|
|
|
37
|
|
|
const AUTH_DOMAIN = 'https://accounts.lifeboat.app'; |
38
|
|
|
const SITES_URL = '/oauth/sites'; |
39
|
|
|
|
40
|
|
|
const ACTIVE_HOST_PARAM = 'lb_active_host'; |
41
|
|
|
const ACTIVE_KEY_PARAM = 'lb_active_site_key'; |
42
|
|
|
|
43
|
|
|
protected $_auth_domain = 'https://accounts.lifeboat.app'; |
44
|
|
|
protected $_access_token = ''; |
45
|
|
|
protected $_site_key = ''; |
46
|
|
|
protected $_host = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
* |
51
|
|
|
* @throws OAuthException |
52
|
|
|
*/ |
53
|
|
|
abstract public function getAccessToken(): string; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $service |
57
|
|
|
* @return ApiService|null |
58
|
|
|
* @throws BadMethodException |
59
|
|
|
*/ |
60
|
|
|
public function __get(string $service): ?ApiService |
61
|
|
|
{ |
62
|
|
|
$obj = ServiceFactory::inst($this, $service); |
63
|
|
|
if (!$obj) throw new BadMethodException("Service for `{$service}` does not exist"); |
64
|
|
|
|
65
|
|
|
return $obj; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getAuthDomain(): string |
72
|
|
|
{ |
73
|
|
|
return $this->_auth_domain; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $host |
78
|
|
|
* @param string $site_key |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setActiveSite(string $host, string $site_key): Connector |
82
|
|
|
{ |
83
|
|
|
$this->_host = $host; |
84
|
|
|
$this->_site_key = $site_key; |
85
|
|
|
|
86
|
|
|
if (session_status() === PHP_SESSION_ACTIVE) { |
87
|
|
|
$_SESSION[self::ACTIVE_HOST_PARAM] = $this->_host; |
88
|
|
|
$_SESSION[self::ACTIVE_KEY_PARAM] = $this->_site_key; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param bool $check_session |
96
|
|
|
* @return array|null |
97
|
|
|
*/ |
98
|
|
|
public function getActiveSite(bool $check_session = true): ?array |
99
|
|
|
{ |
100
|
|
|
if ($this->_host && $this->_site_key) { |
101
|
|
|
return ['host' => $this->_host, 'site_key' => $this->_site_key]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($check_session && session_status() === PHP_SESSION_ACTIVE) { |
105
|
|
|
$this->setActiveSite( |
106
|
|
|
$_SESSION[self::ACTIVE_HOST_PARAM] ?? '', |
107
|
|
|
$_SESSION[self::ACTIVE_KEY_PARAM] ?? '' |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
return $this->getActiveSite(false); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return string|null |
118
|
|
|
*/ |
119
|
|
|
public function getHost(): ?string |
120
|
|
|
{ |
121
|
|
|
if (!$this->getActiveSite(true)) return null; |
122
|
|
|
return $this->_host; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
|
|
public function getSiteKey(): ?string |
129
|
|
|
{ |
130
|
|
|
if (!$this->getActiveSite(true)) return null; |
131
|
|
|
return $this->_site_key; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $url |
136
|
|
|
* @param string $method |
137
|
|
|
* @param array $data |
138
|
|
|
* @param array $headers |
139
|
|
|
* @return CurlResponse |
140
|
|
|
* @throws OAuthException |
141
|
|
|
*/ |
142
|
|
|
public function curl_api(string $url, string $method = 'GET', array $data = [], array $headers = []): CurlResponse |
143
|
|
|
{ |
144
|
|
|
$url = URL::is_absolute_url($url) ? $url |
145
|
|
|
: 'https://' . rtrim($this->getHost(), '/') . '/' . ltrim($url, '/'); |
|
|
|
|
146
|
|
|
|
147
|
|
|
$curl = new Curl($url, $data, $headers); |
148
|
|
|
|
149
|
|
|
$curl->setMethod($method); |
150
|
|
|
$curl->addHeader('Accept', 'application/json'); |
151
|
|
|
$curl->addHeader('Host', $this->getHost()); |
|
|
|
|
152
|
|
|
|
153
|
|
|
foreach ($this->getAuthHeaders() as $header => $value) { |
154
|
|
|
if ($value) $curl->addHeader($header, $value); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $curl->curl_json(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
* @throws OAuthException |
163
|
|
|
*/ |
164
|
|
|
public function getAuthHeaders(): array |
165
|
|
|
{ |
166
|
|
|
return [ |
167
|
|
|
'access-token' => $this->getAccessToken(), |
168
|
|
|
'site-key' => $this->getSiteKey() |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param string $path |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
protected function auth_url(string $path): string |
177
|
|
|
{ |
178
|
|
|
return rtrim($this->_auth_domain, '/') . '/' . ltrim($path, '/'); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|