Completed
Push — master ( ab94e9...fa2f9e )
by Gabriel
02:21
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 5
    public function __construct(string $clientId, string $clientSecret, string $accessToken = null)
23
    {
24 5
        $this->apiUrl = self::PRODUCTION_API_URL;
25 5
        $this->requestHandler = new RequestHandler($accessToken, $this->apiUrl);
26 5
        $this->clientId = $clientId;
27 5
        $this->clientSecret = $clientSecret;
28 5
        $this->accessToken = $accessToken;
29 5
        $this->products = new Products($this->requestHandler);
30 5
    }
31
32
    public function getApiUrl()
33
    {
34
        return $this->apiUrl;
35
    }
36
37 5
    public function setApiUrl($apiUrl)
38
    {
39 5
        $this->apiUrl = rtrim($apiUrl, '/');
40 5
        $this->requestHandler->setApiUrl($this->apiUrl);
41 5
    }
42
43
    public function getClientId(): string
44
    {
45
        return $this->clientId;
46
    }
47
48 1
    public function setClientId(string $clientId = null)
49
    {
50 1
        $this->clientId = $clientId;
51 1
    }
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 2
    public function getAccessToken(): string
64
    {
65 2
        if (null !== $this->accessToken) {
66 1
            return $this->accessToken;
67
        }
68 1
        $response = $this->requestHandler->post(
69 1
            '/v1/authorize',
70
            [
71 1
                'client_id' => $this->clientId,
72 1
                'client_secret' => $this->clientSecret,
73 1
                '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 5
    public function setAccessToken(string $accessToken = null)
82
    {
83 5
        $this->accessToken = $accessToken;
84 5
        $this->requestHandler->setAccessToken($this->accessToken);
85 5
    }
86
87 5
    public function setMockHandler(HandlerStack $mockHandler = null)
88
    {
89 5
        $this->requestHandler->setMockHandler($mockHandler);
90 5
    }
91
}
92