Passed
Push — master ( 8f82d1...61929e )
by Ondra
02:08 queued 57s
created

PaymentConfigProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
    public function __construct(string $defaultGateway = 'default')
35
    {
36
        $this->defaultGateway = $defaultGateway;
37
    }
38
39
    public function addPaymentConfig(PaymentConfig $paymentConfig): void
40
    {
41
        $this->paymentConfigs[$paymentConfig->getGateway()] = $paymentConfig;
42
    }
43
44
45
    public function getUrl(string|null $gateway = null): string
46
    {
47
        return $this->paymentConfigs[$this->getGateway($gateway)]->getUrl();
48
    }
49
50
    public function getMerchantNumber(string|null $gateway = null): MerchantNumber
51
    {
52
        return $this->paymentConfigs[$this->getGateway($gateway)]->getMerchantNumber();
53
    }
54
55
    public function getDepositFlag(string|null $gateway = null): DepositFlag
56
    {
57
        return $this->paymentConfigs[$this->getGateway($gateway)]->getDepositFlag();
58
    }
59
60
    public function getDefaultGateway(): string
61
    {
62
        return $this->defaultGateway;
63
    }
64
65
    public function setDefaultGateway(string $gateway): void
66
    {
67
        $this->defaultGateway = $gateway;
68
    }
69
70
    public function getGateway(?string $gateway = null): string
71
    {
72
        if (null === $gateway) {
73
            $gateway = $this->getDefaultGateway();
74
        }
75
76
        if (!array_key_exists($gateway, $this->paymentConfigs)) {
77
            throw new InvalidArgumentException(
78
                sprintf(
79
                    'Config for key: "%s" not exist. Possible keys are: "%s"',
80
                    $gateway,
81
                    implode(', ', array_keys($this->paymentConfigs))
82
                )
83
            );
84
        }
85
86
        return $gateway;
87
    }
88
89
    public function getResponseUrl(?string $gateway = null): ?ResponseUrl
90
    {
91
        return $this->paymentConfigs[$this->getGateway($gateway)]->getResponseUrl();
92
    }
93
}
94