Passed
Push — master ( 3620ed...dc61a9 )
by Ondra
28:14 queued 09:36
created

ConfigFactory::normalizeParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 19
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\Factory;
16
17
use Pixidos\GPWebPay\Config\Config;
18
use Pixidos\GPWebPay\Config\PaymentConfigProvider;
19
use Pixidos\GPWebPay\Config\SignerConfig;
20
use Pixidos\GPWebPay\Config\SignerConfigProvider;
21
use Pixidos\GPWebPay\Exceptions\InvalidArgumentException;
22
23
class ConfigFactory implements ConfigFactoryInterface
24
{
25
    public const PRIVATE_KEY = 'privateKey';
26
    public const URL = 'url';
27
    public const MERCHANT_NUMBER = 'merchantNumber';
28
    public const DEPOSIT_FLAG = 'depositFlag';
29
    public const PRIVATE_KEY_PASSPHRASE = 'privateKeyPassphrase';
30
    public const PUBLIC_KEY = 'publicKey';
31
    public const RESPONSE_URL = 'responseUrl';
32
33
    private PaymentConfigFactory $paymentConfigFactory;
34
35
    public function __construct(PaymentConfigFactory $paymentConfigFactory)
36
    {
37
        $this->paymentConfigFactory = $paymentConfigFactory;
38
    }
39
40
    /**
41
     * @param array<string, mixed> $params
42
     * @param string  $defaultGateway
43
     * @return Config
44
     * @phpstan-ignore-next-line
45
     */
46
    public function create(array $params, string $defaultGateway = 'default'): Config
47
    {
48
        $data = $this->normalizeParams($params, $defaultGateway);
49
        $config = $this->createConfig($defaultGateway);
50
        $this->processParams($data, $config);
51
52
        return $config;
53
    }
54
55
    /**
56
     * @param array<string, mixed>  $params
57
     * @param string $defaultGateway
58
     * @return array<string, array<string, string|int>>
59
     */
60
    private function normalizeParams(array $params, string $defaultGateway): array
61
    {
62
        $defaultGateway = strtolower($defaultGateway);
63
64
        if (!array_key_exists(self::PRIVATE_KEY, $params)) {
65
            //not multiple config
66
67
            /**
68
             * @phpstan-ignore-next-line
69
             */
70
            return $params;
71
        }
72
        $data = [];
73
        $data[$defaultGateway] = $params;
74
75
        /**
76
         * @phpstan-ignore-next-line
77
         */
78
        return $data;
79
    }
80
81
82
    /**
83
     * @param string                    $key
84
     * @param array<string, int|string> $data
85
     * @param string                    $gateway
86
     *
87
     * @return int|string
88
     * @throws InvalidArgumentException
89
     */
90
    private function getValue(string $key, array $data, string $gateway)
91
    {
92
        if (!array_key_exists($key, $data)) {
93
            throw new InvalidArgumentException(sprintf('Missing key:"%s" in %s configuration', $key, $gateway));
94
        }
95
96
        return $data[$key];
97
    }
98
99
100
    /**
101
     * @param string $defaultGateway
102
     * @return Config
103
     */
104
    private function createConfig(string $defaultGateway): Config
105
    {
106
        $paymentConfigProvider = new PaymentConfigProvider();
107
        $paymentConfigProvider->setDefaultGateway($defaultGateway);
108
        $signerConfigProvider = new SignerConfigProvider();
109
110
        return new Config($paymentConfigProvider, $signerConfigProvider);
111
    }
112
113
    /**
114
     * @param array<array<string, int|string>> $params
115
     * @param Config                           $config
116
     */
117
    private function processParams(array $params, Config $config): void
118
    {
119
        $paymentConfig = $config->getPaymentConfigProvider();
120
        $signerConfig = $config->getSignerConfigProvider();
121
        foreach ($params as $gateway => $data) {
122
            $paymentConfig->addPaymentConfig(
123
                $this->paymentConfigFactory->create(
124
                    (string)$this->getValue(self::URL, $data, $gateway),
125
                    (string)$this->getValue(self::MERCHANT_NUMBER, $data, $gateway),
126
                    array_key_exists(self::DEPOSIT_FLAG, $data) ? (int)$data[self::DEPOSIT_FLAG] : 1,
127
                    $gateway,
128
                    (string)($data[self::RESPONSE_URL] ?? null)
129
                )
130
            );
131
            $signerConfig->addConfig(
132
                new SignerConfig(
133
                    (string)$this->getValue(self::PRIVATE_KEY, $data, $gateway),
134
                    (string)$this->getValue(self::PRIVATE_KEY_PASSPHRASE, $data, $gateway),
135
                    (string)$this->getValue(self::PUBLIC_KEY, $data, $gateway)
136
                ),
137
                $gateway
138
            );
139
        }
140
    }
141
}
142