Completed
Push — master ( 9e30d1...c7ffd5 )
by Gabriel
02:32
created

Waredesk::getClientId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Waredesk;
4
5
use GuzzleHttp\HandlerStack;
6
7
class Waredesk
8
{
9
    const PRODUCTION_API_URL = 'https://api.waredesk.com';
10
11
    /**
12
     * @var Products
13
     */
14
    public $products;
15
16
    private $apiUrl;
17
    private $requestHandler;
18
    private $clientId;
19
    private $clientSecret;
20
    private $accessToken;
21
22
    public function __construct(string $clientId, string $clientSecret, string $accessToken = null)
23
    {
24
        $this->apiUrl = self::PRODUCTION_API_URL;
25
        $this->requestHandler = new RequestHandler($accessToken, $this->apiUrl);
26
        $this->clientId = $clientId;
27
        $this->clientSecret = $clientSecret;
28
        $this->accessToken = $accessToken;
29
        $this->products = new Products();
30
    }
31
32
    public function getApiUrl()
33
    {
34
        return $this->apiUrl;
35
    }
36
37
    public function setApiUrl($apiUrl)
38
    {
39
        $this->apiUrl = rtrim($apiUrl, '/');
40
        $this->requestHandler->setApiUrl($this->apiUrl);
41
    }
42
43
    public function getClientId(): string
44
    {
45
        return $this->clientId;
46
    }
47
48
    public function setClientId(string $clientId = null)
49
    {
50
        $this->clientId = $clientId;
51
    }
52
53
    public function getClientSecret(): string
54
    {
55
        return $this->clientSecret;
56
    }
57
58
    public function setClientSecret(string $clientSecret = null)
59
    {
60
        $this->clientSecret = $clientSecret;
61
    }
62
63
    public function getAccessToken(): string
64
    {
65
        if (null !== $this->accessToken) {
66
            return $this->accessToken;
67
        }
68
        $response = $this->requestHandler->post(
69
            '/v1/authorize',
70
            [
71
                'client_id' => $this->clientId,
72
                'client_secret' => $this->clientSecret,
73
                'grant_type' => 'client_credentials'
74
            ]
75
        );
76
        $this->accessToken = $response['access_token'];
77
        $this->requestHandler->setAccessToken($this->accessToken);
78
        return $this->accessToken;
79
    }
80
81
    public function setAccessToken(string $accessToken = null)
82
    {
83
        $this->accessToken = $accessToken;
84
        $this->requestHandler->setAccessToken($this->accessToken);
85
    }
86
87
    public function setMockHandler(HandlerStack $mockHandler = null)
88
    {
89
        $this->requestHandler->setMockHandler($mockHandler);
90
    }
91
}
92