Passed
Push — master ( 99d0e8...9e7049 )
by Ondra
03:35
created

PaymentConfigProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getDepositFlag() 0 3 1
A getResponseUrl() 0 3 1
A getMerchantNumber() 0 3 1
A __construct() 0 3 1
A getUrl() 0 3 1
A getDefaultGateway() 0 3 1
A setDefaultGateway() 0 3 1
A getGateway() 0 17 3
A addPaymentConfig() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the Pixidos package.
5
 *
6
 *  (c) Ondra Votava <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 *
11
 */
12
13
declare(strict_types=1);
14
15
namespace Pixidos\GPWebPay\Config;
16
17
use Pixidos\GPWebPay\Exceptions\InvalidArgumentException;
18
use Pixidos\GPWebPay\Exceptions\LogicException;
19
use Pixidos\GPWebPay\Param\DepositFlag;
20
use Pixidos\GPWebPay\Param\MerchantNumber;
21
use Pixidos\GPWebPay\Param\ResponseUrl;
22
23
class PaymentConfigProvider
24
{
25
    /**
26
     * @var string defaultGatewayKey
27
     */
28
    private string $defaultGateway;
29
    /**
30
     * @var array<PaymentConfig>
31
     */
32
    private array $paymentConfigs = [];
33
34 10
    public function __construct(string $defaultGateway = 'default')
35
    {
36 10
        $this->defaultGateway = $defaultGateway;
37
    }
38
39 10
    public function addPaymentConfig(PaymentConfig $paymentConfig): void
40
    {
41 10
        $this->paymentConfigs[$paymentConfig->getGateway()] = $paymentConfig;
42
    }
43
44
45 11
    public function getUrl(string|null $gateway = null): string
46
    {
47 11
        return $this->paymentConfigs[$this->getGateway($gateway)]->getUrl();
48
    }
49
50 16
    public function getMerchantNumber(string|null $gateway = null): MerchantNumber
51
    {
52 16
        return $this->paymentConfigs[$this->getGateway($gateway)]->getMerchantNumber();
53
    }
54
55 10
    public function getDepositFlag(string|null $gateway = null): DepositFlag
56
    {
57 10
        return $this->paymentConfigs[$this->getGateway($gateway)]->getDepositFlag();
58
    }
59
60 17
    public function getDefaultGateway(): string
61
    {
62 17
        return $this->defaultGateway;
63
    }
64
65 1
    public function setDefaultGateway(string $gateway): void
66
    {
67 1
        $this->defaultGateway = $gateway;
68
    }
69
70 18
    public function getGateway(?string $gateway = null): string
71
    {
72 18
        if (null === $gateway) {
73 8
            $gateway = $this->getDefaultGateway();
74
        }
75
76 18
        if (!array_key_exists($gateway, $this->paymentConfigs)) {
77 1
            throw new InvalidArgumentException(
78 1
                sprintf(
79 1
                    'Config for key: "%s" not exist. Possible keys are: "%s"',
80 1
                    $gateway,
81 1
                    implode(', ', array_keys($this->paymentConfigs))
82 1
                )
83 1
            );
84
        }
85
86 17
        return $gateway;
87
    }
88
89 6
    public function getResponseUrl(?string $gateway = null): ?ResponseUrl
90
    {
91 6
        return $this->paymentConfigs[$this->getGateway($gateway)]->getResponseUrl();
92
    }
93
}
94