Client::getApiBaseUrl()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright © Thomas Klein, All rights reserved.
4
 * See LICENSE bundled with this library for license details.
5
 */
6
declare(strict_types=1);
7
8
namespace Zoho\Desk\OAuth;
9
10
use Zoho\OAuth\Exception\ZohoOAuthException;
11
use Zoho\OAuth\ZohoOAuth;
12
use Zoho\OAuth\ZohoOAuthClient;
13
use Zoho\Desk\Api\Metadata;
14
use Zoho\Desk\Client\ConfigProviderInterface;
15
use Zoho\Desk\Exception\Exception;
16
17
final class Client implements ClientInterface
18
{
19
    private ConfigProviderInterface $configProvider;
20
21
    private bool $isConfigured;
22
23
    public function __construct(
24
        ConfigProviderInterface $configProvider
25
    ) {
26
        $this->configProvider = $configProvider;
27
        $this->isConfigured = false;
28
    }
29
30
    public function getApiBaseUrl(): string
31
    {
32
        return $this->configProvider->get()[Metadata::API_FIELD_BASE_URL] ?? Metadata::API_ENDPOINT_US;
33
    }
34
35
    public function getApiVersion(): string
36
    {
37
        return $this->configProvider->get()[Metadata::API_FIELD_VERSION] ?? Metadata::API_VERSION;
38
    }
39
40
    /**
41
     * @return string
42
     * @throws Exception
43
     */
44
    public function getAccessToken(): string
45
    {
46
        try {
47
            $this->configure();
48
            /** @var ZohoOAuthClient $oauthClient */
49
            $oauthClient = ZohoOAuth::getClientInstance();
50
            $accessToken = $oauthClient->getAccessToken($this->configProvider->get()[Metadata::API_FIELD_CURRENT_USER_EMAIL]);
51
        } catch (ZohoOAuthException $e) {
52
            throw new Exception($e->getMessage(), $e->getCode(), $e);
53
        }
54
55
        return $accessToken;
56
    }
57
58
    public function getOrgId(): int
59
    {
60
        return (int) $this->configProvider->get()[Metadata::ORG_ID];
61
    }
62
63
    private function configure(): void
64
    {
65
        if (!$this->isConfigured) {
66
            ZohoOAuth::initialize($this->configProvider->get());
67
            $this->isConfigured = true;
68
        }
69
    }
70
}
71