ClientBuilder   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A setEnvironment() 0 9 1
A setLogger() 0 9 2
A getLogger() 0 7 1
A build() 0 7 2
1
<?php
2
declare(strict_types=1);
3
4
namespace MerchantSafeUnipay\SDK;
5
6
use Monolog\ErrorHandler;
7
use Psr\Log\LoggerInterface;
8
use Monolog\Logger;
9
use Monolog\Handler\ErrorLogHandler;
10
use GuzzleHttp;
11
use MerchantSafeUnipay\SDK\Client;
12
use MerchantSafeUnipay\SDK\Exception\InvalidArgumentException;
13
14
final class ClientBuilder
15
{
16
    private $environment;
17
    private $logger;
18
19
20
    public static function create()
21
    {
22
        return new static();
23
    }
24
25
    public function setEnvironment(
26
        string $apiUrl,
27
        string $merchant,
28
        string $merchantUser,
29
        string $merchantPassword
30
    ) {
31
        $this->environment = new Environment($apiUrl, $merchant, $merchantUser, $merchantPassword);
32
        return $this;
33
    }
34
35
    public function setLogger($logger = null)
36
    {
37
        if ($logger instanceof LoggerInterface) {
38
            $this->logger = $logger;
39
            return $this;
40
        }
41
        $this->logger = $this->getLogger();
42
        return $this;
43
    }
44
    private function getLogger()
45
    {
46
47
        $log = new Logger('MerchantSafeUnipayPhpSDK');
48
        $log->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING));
49
        return $log;
50
    }
51
52
    public function build()
53
    {
54
        if ($this->logger === null) {
55
            $this->logger = $this->getLogger();
56
        }
57
        return new Client($this->environment, new GuzzleHttp\Client(), $this->logger);
58
    }
59
}
60