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

ConfigFactory::createConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
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
/**
24
 * @phpstan-import-type ConfigParams from ConfigFactoryInterface
25
 * @phpstan-import-type GatewayConfig from ConfigFactoryInterface
26
 */
27
class ConfigFactory implements ConfigFactoryInterface
28
{
29
    private PaymentConfigFactory $paymentConfigFactory;
30
31 9
    public function __construct(PaymentConfigFactory $paymentConfigFactory)
32
    {
33 9
        $this->paymentConfigFactory = $paymentConfigFactory;
34
    }
35
36 9
    public function create(array $params, string $defaultGateway = 'default'): Config
37
    {
38 9
        $data = $this->normalizeParams($params, $defaultGateway);
39 8
        $config = $this->createConfig($defaultGateway);
40 8
        $this->processParams($data, $config);
41
42 8
        return $config;
43
    }
44
45
    /**
46
     * @phpstan-param ConfigParams $params
47
     * @return array<string, GatewayConfig>
48
     */
49 9
    private function normalizeParams(array $params, string $defaultGateway): array
50
    {
51 9
        $defaultGateway = strtolower($defaultGateway);
52
53
        // Pokud je to přímo GatewayConfig (ne array<string, GatewayConfig>)
54 9
        if (array_key_exists(self::PRIVATE_KEY, $params)) {
55
            /** @var GatewayConfig $params */
56 3
            return [$defaultGateway => $params];
57
        }
58
59 6
        if (!array_key_exists($defaultGateway, $params)) {
60 1
            throw new InvalidArgumentException(
61 1
                sprintf(
62 1
                    'The key for defautlGateway: "%s" is not included in the configuration parameters. Keys in the parameters "%s"',
63 1
                    $defaultGateway,
64 1
                    implode(', ', array_keys($params))
65 1
                )
66 1
            );
67
        }
68
69
        /** @phpstan-var array<string, GatewayConfig> $params */
70 5
        return $params;
71
    }
72
73 8
    private function createConfig(string $defaultGateway): Config
74
    {
75 8
        $paymentConfigProvider = new PaymentConfigProvider($defaultGateway);
76 8
        $signerConfigProvider = new SignerConfigProvider();
77 8
        $signerConfigProvider->setDefaultGateway($defaultGateway);
78
79 8
        return new Config($paymentConfigProvider, $signerConfigProvider);
80
    }
81
82
    /**
83
     * @phpstan-param array<string, GatewayConfig> $params
84
     */
85 8
    private function processParams(array $params, Config $config): void
86
    {
87 8
        $paymentConfig = $config->getPaymentConfigProvider();
88 8
        $signerConfig = $config->getSignerConfigProvider();
89 8
        foreach ($params as $gateway => $data) {
90 8
            $paymentConfig->addPaymentConfig(
91 8
                $this->paymentConfigFactory->create(
92 8
                    $data[self::URL],
93 8
                    $data[self::MERCHANT_NUMBER],
94 8
                    $data[self::DEPOSIT_FLAG],
95 8
                    $gateway,
96 8
                    (string)($data[self::RESPONSE_URL] ?? null)
97 8
                )
98 8
            );
99 8
            $signerConfig->addConfig(
100 8
                new SignerConfig(
101 8
                    $data[self::PRIVATE_KEY],
102 8
                    $data[self::PRIVATE_KEY_PASSPHRASE],
103 8
                    $data[self::PUBLIC_KEY]
104 8
                ),
105 8
                $gateway
106 8
            );
107
        }
108
    }
109
}
110