Test Failed
Branch master (a8f59d)
by Ondra
04:16
created

ConfigFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createConfig() 0 7 1
A create() 0 7 1
A processParams() 0 21 2
A normalizeParams() 0 12 2
A __construct() 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\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
    public function __construct(PaymentConfigFactory $paymentConfigFactory)
32
    {
33
        $this->paymentConfigFactory = $paymentConfigFactory;
34
    }
35
36
    public function create(array $params, string $defaultGateway = 'default'): Config
37
    {
38
        $data = $this->normalizeParams($params, $defaultGateway);
39
        $config = $this->createConfig($defaultGateway);
40
        $this->processParams($data, $config);
41
42
        return $config;
43
    }
44
45
    /**
46
     * @param ConfigParams $params
0 ignored issues
show
Bug introduced by
The type Pixidos\GPWebPay\Config\Factory\ConfigParams was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     * @return array<string, GatewayConfig>
48
     */
49
    private function normalizeParams(array $params, string $defaultGateway): array
50
    {
51
        $defaultGateway = strtolower($defaultGateway);
52
53
        // Pokud je to přímo GatewayConfig (ne array<string, GatewayConfig>)
54
        if (array_key_exists(self::PRIVATE_KEY, $params)) {
55
            /** @var GatewayConfig $params */
56
            return [$defaultGateway => $params];
57
        }
58
59
        /** @var array<string, GatewayConfig> $params */
60
        return $params;
61
    }
62
63
64
    /**
65
     * @param string $defaultGateway
66
     * @return Config
67
     */
68
    private function createConfig(string $defaultGateway): Config
69
    {
70
        $paymentConfigProvider = new PaymentConfigProvider();
71
        $paymentConfigProvider->setDefaultGateway($defaultGateway);
72
        $signerConfigProvider = new SignerConfigProvider();
73
74
        return new Config($paymentConfigProvider, $signerConfigProvider);
75
    }
76
77
    /**
78
     * @param array<string, GatewayConfig> $params
79
     * @param Config                       $config
80
     */
81
    private function processParams(array $params, Config $config): void
82
    {
83
        $paymentConfig = $config->getPaymentConfigProvider();
84
        $signerConfig = $config->getSignerConfigProvider();
85
        foreach ($params as $gateway => $data) {
86
            $paymentConfig->addPaymentConfig(
87
                $this->paymentConfigFactory->create(
88
                    $data[self::URL],
89
                    $data[self::MERCHANT_NUMBER],
90
                    $data[self::DEPOSIT_FLAG],
91
                    $gateway,
92
                    (string)($data[self::RESPONSE_URL] ?? null)
93
                )
94
            );
95
            $signerConfig->addConfig(
96
                new SignerConfig(
97
                    $data[self::PRIVATE_KEY],
98
                    $data[self::PRIVATE_KEY_PASSPHRASE],
99
                    $data[self::PUBLIC_KEY]
100
                ),
101
                $gateway
102
            );
103
        }
104
    }
105
}
106