1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Waredesk; |
4
|
|
|
|
5
|
|
|
class Waredesk |
6
|
|
|
{ |
7
|
|
|
const PRODUCTION_API_URL = 'https://api.waredesk.com'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var Products |
11
|
|
|
*/ |
12
|
|
|
public $products; |
13
|
|
|
|
14
|
|
|
private $apiUrl; |
15
|
|
|
private $requestHandler; |
16
|
|
|
private $clientId; |
17
|
|
|
private $clientSecret; |
18
|
|
|
private $accessToken; |
19
|
|
|
|
20
|
|
|
public function __construct(string $clientId, string $clientSecret, string $accessToken = null) |
21
|
|
|
{ |
22
|
|
|
$this->apiUrl = self::PRODUCTION_API_URL; |
23
|
|
|
$this->requestHandler = new RequestHandler($accessToken, $this->apiUrl); |
24
|
|
|
$this->clientId = $clientId; |
25
|
|
|
$this->clientSecret = $clientSecret; |
26
|
|
|
$this->accessToken = $accessToken; |
27
|
|
|
$this->products = new Products(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getApiUrl() |
31
|
|
|
{ |
32
|
|
|
return $this->apiUrl; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function setApiUrl($apiUrl) |
36
|
|
|
{ |
37
|
|
|
$this->apiUrl = rtrim($apiUrl, '/'); |
38
|
|
|
$this->requestHandler->setApiUrl($this->apiUrl); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getAccessToken(): string |
42
|
|
|
{ |
43
|
|
|
if (null !== $this->accessToken) { |
44
|
|
|
return $this->accessToken; |
45
|
|
|
} |
46
|
|
|
$response = $this->requestHandler->post( |
47
|
|
|
'/v1/authorize', |
48
|
|
|
[ |
49
|
|
|
'client_id' => $this->clientId, |
50
|
|
|
'client_secret' => $this->clientSecret, |
51
|
|
|
'grant_type' => 'client_credentials' |
52
|
|
|
] |
53
|
|
|
); |
54
|
|
|
$this->accessToken = $response['access_token']; |
55
|
|
|
$this->requestHandler->setAccessToken($this->accessToken); |
56
|
|
|
return $this->accessToken; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setAccessToken(string $accessToken) |
60
|
|
|
{ |
61
|
|
|
$this->accessToken = $accessToken; |
62
|
|
|
$this->requestHandler->setAccessToken($this->accessToken); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|