|
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
|
|
|
* Makes a request to the API to refresh the current access token |
|
65
|
|
|
* @see Client::getAccessToken() |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
* @throws OAuthException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function refreshAccessToken(): Client |
|
71
|
|
|
{ |
|
72
|
|
|
$this->_access_token = null; |
|
73
|
|
|
$this->getAccessToken(); |
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getAPIKey(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->_api_key; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getAPISecret(): string |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->_api_secret; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
private function auth_url(string $path): string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->_auth_domain . '/' . ltrim($path, '/'); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|