Passed
Push — master ( dc4f1f...6b2c4c )
by Gabriel
02:50
created

Waredesk::setAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
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