ClientBuilder::getLogger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API;
5
6
use Psr\Log\LoggerInterface;
7
use Monolog\Logger;
8
use Monolog\Handler\ErrorLogHandler;
9
use GuzzleHttp;
10
11
final class ClientBuilder
12
{
13
    private $environment;
14
    private $logger;
15
16
    public static function create()
17
    {
18
        return new static();
19
    }
20
21
    public function setEnvironment(
22
        string $apiUrl,
23
        string $apiKey,
24
        string $apiSecret,
25
        ?string $clientUsername = null,
26
        ?string $appName = null,
27
        ?string $channel = null
28
    ) {
29
        $this->environment = new Environment($apiUrl, $apiKey, $apiSecret, $clientUsername, $appName, $channel);
30
        return $this;
31
    }
32
33
    public function setLogger($logger = null)
34
    {
35
        if ($logger instanceof LoggerInterface) {
36
            $this->logger = $logger;
37
            return $this;
38
        }
39
        $this->logger = $this->getLogger();
40
        return $this;
41
    }
42
43
    private function getLogger() : Logger
44
    {
45
        $log = new Logger('API-API');
46
        $log->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::DEBUG));
47
        return $log;
48
    }
49
50
    public function build() : Client
51
    {
52
        if ($this->logger === null) {
53
            $this->logger = $this->getLogger();
54
        }
55
        return new Client($this->environment, new GuzzleHttp\Client(), $this->logger);
56
    }
57
}
58