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_sdk_active_host'; |
||||
41 | const ACTIVE_KEY_PARAM = 'lb_sdk_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 | $uri = URL::is_absolute_url($url) ? $url |
||||
145 | : 'https://' . rtrim($this->getHost(), '/') . '/' . ltrim($url, '/'); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
146 | |||||
147 | $curl = new Curl($uri, $data, $headers); |
||||
148 | |||||
149 | $curl->setMethod($method); |
||||
150 | $curl->addHeader('Accept', 'application/json'); |
||||
151 | $curl->addHeader('Host', $this->getHost()); |
||||
0 ignored issues
–
show
It seems like
$this->getHost() can also be of type null ; however, parameter $value of Lifeboat\Utils\Curl::addHeader() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
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 | if (!$this->getAccessToken()) $this->fetchAccessToken(); |
||||
0 ignored issues
–
show
The method
fetchAccessToken() does not exist on Lifeboat\Connector . Since it exists in all sub-types, consider adding an abstract or default implementation to Lifeboat\Connector .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
167 | if (!$this->getAccessToken()) throw new OAuthException("Access token has not been retreived"); |
||||
168 | |||||
169 | return [ |
||||
170 | 'access-token' => $this->getAccessToken(), |
||||
171 | 'site-key' => $this->getSiteKey() |
||||
172 | ]; |
||||
173 | } |
||||
174 | |||||
175 | /** |
||||
176 | * @param string $path |
||||
177 | * @return string |
||||
178 | */ |
||||
179 | protected function auth_url(string $path): string |
||||
180 | { |
||||
181 | return rtrim($this->_auth_domain, '/') . '/' . ltrim($path, '/'); |
||||
182 | } |
||||
183 | } |
||||
184 |