ApiKeysTestCreator::create()   B
last analyzed

Complexity

Conditions 8
Paths 4

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 2
b 0
f 0
nc 4
nop 2
dl 0
loc 29
rs 8.4444
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Creator;
13
14
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
15
use BitBag\SyliusMolliePlugin\DTO\ApiKeyTest;
16
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
final class ApiKeysTestCreator implements ApiKeysTestCreatorInterface
20
{
21
    /** @var MollieApiClient */
22
    private $mollieApiClient;
23
24
    /** @var TranslatorInterface */
25
    private $translator;
26
27
    public function __construct(
28
        MollieApiClient $mollieApiClient,
29
        TranslatorInterface $translator
30
    ) {
31
        $this->mollieApiClient = $mollieApiClient;
32
        $this->translator = $translator;
33
    }
34
35
    public function create(string $keyType, string $key = null): ApiKeyTest
36
    {
37
        $apiKeyTest = new ApiKeyTest(
38
            $keyType,
39
            $key ? true : false,
40
        );
41
42
        if (null === $key || empty(trim($key))) {
43
            $apiKeyTest->setStatus(self::ERROR_STATUS);
44
            $apiKeyTest->setMessage($this->translator->trans('bitbag_sylius_mollie_plugin.ui.inser_you_key_first'));
45
46
            return $apiKeyTest;
47
        }
48
49
        if ($apiKeyTest->getType() === MollieGatewayConfigurationType::API_KEY_TEST && !str_starts_with($key, self::TEST_PREFIX)) {
50
            $apiKeyTest->setStatus(self::ERROR_STATUS);
51
            $apiKeyTest->setMessage($this->translator->trans('bitbag_sylius_mollie_plugin.ui.api_key_start_with_api_key_test'));
52
53
            return $apiKeyTest;
54
        }
55
56
        if ($apiKeyTest->getType() === MollieGatewayConfigurationType::API_KEY_LIVE && !str_starts_with($key, self::LIVE_PREFIX)) {
57
            $apiKeyTest->setStatus(self::ERROR_STATUS);
58
            $apiKeyTest->setMessage($this->translator->trans('bitbag_sylius_mollie_plugin.ui.api_key_start_with_api_key_live'));
59
60
            return $apiKeyTest;
61
        }
62
63
        return $this->testApiKey($apiKeyTest, $key);
64
    }
65
66
    private function testApiKey(ApiKeyTest $apiKeyTest, string $apiKey): ApiKeyTest
67
    {
68
        try {
69
            $client = $this->mollieApiClient->setApiKey($apiKey);
70
71
            $methods = $client->methods->allActive(MollieMethodsCreatorInterface::PARAMETERS);
72
            $apiKeyTest->setMethods($methods);
73
74
            return $apiKeyTest;
75
        } catch (\Exception $exception) {
76
            $apiKeyTest->setStatus(self::ERROR_STATUS);
77
78
            if ($exception->getCode() === 0) {
79
                $apiKeyTest->setMessage($this->translator->trans(
80
                    \sprintf('bitbag_sylius_mollie_plugin.ui.api_key_start_with_%s', $apiKeyTest->getType())
81
                ));
82
83
                return $apiKeyTest;
84
            }
85
86
            $apiKeyTest->setMessage($this->translator->trans(''));
87
88
            return $apiKeyTest;
89
        }
90
    }
91
}
92