IproSoftwareClient   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 77
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setAccessTokenCacheManager() 0 8 2
A setHttpClient() 0 5 1
A httpClient() 0 3 1
A tryCreateDefaultHttpClient() 0 14 3
A __construct() 0 7 3
1
<?php
2
3
namespace IproSoftwareApi;
4
5
use IproSoftwareApi\AccessToken\NoneCacher;
6
use IproSoftwareApi\Contracts\AccessTokenCacher;
7
use IproSoftwareApi\DTOs\ClientCredentials;
8
use IproSoftwareApi\Exceptions\IproSoftwareApiException;
9
use IproSoftwareApi\Traits\HasApiMethods;
10
11
class IproSoftwareClient
12
{
13
    use HasApiMethods;
14
15
    /** @var \IproSoftwareApi\Contracts\HttpClient */
16
    protected $httpClient;
17
18
    /**
19
     * Client constructor.
20
     *
21
     * @param array $configurations
22
     *
23
     * @throws IproSoftwareApiException
24
     */
25 9
    public function __construct($configurations = [])
26
    {
27 9
        if (isset($configurations['requests_path_prefix']) && $configurations['requests_path_prefix']) {
28 3
            $this->setPathPrefix($configurations['requests_path_prefix']);
29
        }
30
31 9
        $this->tryCreateDefaultHttpClient($configurations);
32
    }
33
34
    /**
35
     * @param Contracts\HttpClient $httpClient
36
     *
37
     * @return static
38
     */
39 4
    public function setHttpClient(Contracts\HttpClient $httpClient): static
40
    {
41 4
        $this->httpClient = $httpClient;
42
43 4
        return $this;
44
    }
45
46
    /**
47
     * @param AccessTokenCacher $cacheManager
48
     *
49
     * @throws IproSoftwareApiException
50
     *
51
     * @return static
52
     */
53 2
    public function setAccessTokenCacheManager(AccessTokenCacher $cacheManager): static
54
    {
55 2
        if (!($this->httpClient instanceof Contracts\HttpClient)) {
0 ignored issues
show
introduced by
$this->httpClient is always a sub-type of IproSoftwareApi\Contracts\HttpClient.
Loading history...
56 1
            throw new IproSoftwareApiException('A HttpClient must be set at the beginning.', 500);
57
        }
58 1
        $this->httpClient->setCacheManager($cacheManager);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * @param array $configurations
65
     *
66
     * @throws IproSoftwareApiException
67
     */
68 9
    protected function tryCreateDefaultHttpClient(array $configurations = [])
69
    {
70 9
        $clientCredentials = new ClientCredentials(
71 9
            $configurations['api_host']      ?? '',
72 9
            $configurations['client_id']     ?? '',
73 9
            $configurations['client_secret'] ?? ''
74 9
        );
75
76 9
        if (isset($configurations['oauth_endpoint'])) {
77 1
            $clientCredentials->tokenEndpoint = $configurations['oauth_endpoint'];
78
        }
79
80 9
        if ($clientCredentials->valid()) {
81 1
            $this->httpClient = new HttpClient($clientCredentials, $configurations['cache_manager'] ?? new NoneCacher(), $configurations);
82
        }
83
    }
84
85 5
    public function httpClient(): ?\IproSoftwareApi\Contracts\HttpClient
86
    {
87 5
        return $this->httpClient;
88
    }
89
}
90