Configuration::setLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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