Configuration   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 5 1
A setLogger() 0 5 1
A __construct() 0 5 1
A setHttpClient() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Services\Client;
6
7
use Http\Client\Common\HttpMethodsClient;
8
use Http\Client\Common\Plugin\AuthenticationPlugin;
9
use Http\Client\Common\Plugin\BaseUriPlugin;
10
use Http\Client\Common\Plugin\LoggerPlugin;
11
use Http\Client\Common\PluginClient;
12
use Http\Client\HttpClient;
13
use Http\Discovery\HttpAsyncClientDiscovery;
14
use Http\Discovery\MessageFactoryDiscovery;
15
use Http\Discovery\UriFactoryDiscovery;
16
use Psr\Log\LoggerInterface;
17
18
final class Configuration
19
{
20
    private $plugins = [];
21
    private $httpClient;
22
23
    public function __construct(string $endpoint, string $token)
24
    {
25
        $this->plugins[] = new BaseUriPlugin((UriFactoryDiscovery::find())->createUri($endpoint));
26
27
        $this->plugins[] = new AuthenticationPlugin(new TokenAuthentication($token));
28
    }
29
30
    public function setLogger(LoggerInterface $logger): self
31
    {
32
        $this->plugins[] = new LoggerPlugin($logger);
33
34
        return $this;
35
    }
36
37
    public function setHttpClient(HttpClient $httpClient): self
38
    {
39
        $this->httpClient = $httpClient;
40
41
        return $this;
42
    }
43
44
    public function getClient(): Client
45
    {
46
        $httpClient = $this->httpClient ?? HttpAsyncClientDiscovery::find();
47
48
        return new Client(new HttpMethodsClient(new PluginClient($httpClient, $this->plugins), MessageFactoryDiscovery::find()));
49
    }
50
}
51