1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\SDK; |
4
|
|
|
|
5
|
|
|
use Lifeboat\SDK\Exceptions\OAuthException; |
6
|
|
|
use Lifeboat\SDK\Services\Curl; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Client |
10
|
|
|
* @package Lifeboat\SDK |
11
|
|
|
*/ |
12
|
|
|
class Client { |
13
|
|
|
|
14
|
|
|
const AUTH_DOMAIN = 'https://accounts.lifeboat.app'; |
15
|
|
|
const TOKEN_URL = '/oauth/api_token'; |
16
|
|
|
const SITES_URL = '/oauth/sites'; |
17
|
|
|
|
18
|
|
|
private $_auth_domain = ''; |
19
|
|
|
private $_api_key = ''; |
20
|
|
|
private $_api_secret = ''; |
21
|
|
|
private $_access_token; |
22
|
|
|
|
23
|
|
|
public function __construct(string $_api_key, string $_api_secret, $_auth_domain = self::AUTH_DOMAIN) |
24
|
|
|
{ |
25
|
|
|
$this->_api_key = $_api_key; |
26
|
|
|
$this->_api_secret = $_api_secret; |
27
|
|
|
$this->_auth_domain = rtrim($_auth_domain, '/'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Tries to fetch an access token from the API based on |
32
|
|
|
* the auth parameters available to client |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
* @throws OAuthException If any error is encountered during OAuth |
36
|
|
|
*/ |
37
|
|
|
public function getAccessToken(): string |
38
|
|
|
{ |
39
|
|
|
if (!$this->_access_token) { |
40
|
|
|
$curl = new Curl($this->auth_url(self::TOKEN_URL), [ |
41
|
|
|
'api_key' => $this->getAPIKey(), |
42
|
|
|
'api_secret' => $this->getAPISecret() |
43
|
|
|
]); |
44
|
|
|
|
45
|
|
|
$curl->setMethod('POST'); |
46
|
|
|
$response = $curl->curl(); |
47
|
|
|
|
48
|
|
|
if (!$response->isValid()) { |
49
|
|
|
throw new OAuthException($response->getRaw()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$json = $response->getJSON(); |
53
|
|
|
if (!array_key_exists('access_token', $json)) { |
54
|
|
|
throw new OAuthException("Access token was not returned by API"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->_access_token = $json['access_token']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->_access_token; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $access_token |
65
|
|
|
* @return array |
66
|
|
|
* @throws OAuthException |
67
|
|
|
*/ |
68
|
|
|
public function getSites(string $access_token = ''): array |
69
|
|
|
{ |
70
|
|
|
if (!$access_token) $access_token = $this->getAccessToken(); |
71
|
|
|
|
72
|
|
|
$curl = new Curl($this->auth_url(self::SITES_URL), [ |
73
|
|
|
'access_token' => $access_token |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$curl->setMethod('POST'); |
77
|
|
|
$response = $curl->curl(); |
78
|
|
|
|
79
|
|
|
if (!$response->isValid()) { |
80
|
|
|
throw new OAuthException($response->getRaw()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $response->getJSON(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Makes a request to the API to refresh the current access token |
88
|
|
|
* @see Client::getAccessToken() |
89
|
|
|
* |
90
|
|
|
* @return $this |
91
|
|
|
* @throws OAuthException |
92
|
|
|
*/ |
93
|
|
|
public function refreshAccessToken(): Client |
94
|
|
|
{ |
95
|
|
|
$this->_access_token = null; |
96
|
|
|
$this->getAccessToken(); |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getAPIKey(): string |
104
|
|
|
{ |
105
|
|
|
return $this->_api_key; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function getAPISecret(): string |
112
|
|
|
{ |
113
|
|
|
return $this->_api_secret; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $path |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function auth_url(string $path): string |
121
|
|
|
{ |
122
|
|
|
return $this->_auth_domain . '/' . ltrim($path, '/'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|