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
|
|
|
static private $validEnvironments = ['Test', 'Production']; |
20
|
|
|
|
21
|
|
|
public static function create() |
22
|
|
|
{ |
23
|
|
|
return new static(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setEnvironment( |
27
|
|
|
string $environment, |
28
|
|
|
string $merchant, |
29
|
|
|
string $merchantUser, |
30
|
|
|
string $merchantPassword |
31
|
|
|
) { |
32
|
|
|
|
33
|
|
|
$environment = ucfirst(strtolower($environment)); |
34
|
|
|
if (!in_array($environment, self::$validEnvironments, true)) { |
35
|
|
|
$message = sprintf('%s is not valid environment.', $environment); |
36
|
|
|
throw new InvalidArgumentException($message); |
37
|
|
|
} |
38
|
|
|
$environmentClass = "\\MerchantSafeUnipay\\SDK\\Environment\\{$environment}"; |
39
|
|
|
$this->environment = new $environmentClass($merchant, $merchantUser, $merchantPassword); |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function setLogger($logger = null) |
44
|
|
|
{ |
45
|
|
|
if ($logger instanceof LoggerInterface) { |
46
|
|
|
$this->logger = $logger; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
$this->logger = $this->getLogger(); |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
private function getLogger() |
53
|
|
|
{ |
54
|
|
|
|
55
|
|
|
$log = new Logger('MerchantSafeUnipayPhpSDK'); |
56
|
|
|
$log->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING)); |
57
|
|
|
return $log; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function build() |
61
|
|
|
{ |
62
|
|
|
if ($this->logger === null) { |
63
|
|
|
$this->logger = $this->getLogger(); |
64
|
|
|
} |
65
|
|
|
return new Client($this->environment, new GuzzleHttp\Client(), $this->logger); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|