MollieApiClientKeyResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 10
rs 10
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\Resolver;
13
14
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
15
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
16
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
17
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
18
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface;
19
use Mollie\Api\Exceptions\ApiException;
20
use Sylius\Component\Channel\Context\ChannelContextInterface;
21
use Sylius\Component\Resource\Exception\UpdateHandlingException;
22
23
final class MollieApiClientKeyResolver implements MollieApiClientKeyResolverInterface
24
{
25
    /** @var MollieApiClient */
26
    private $mollieApiClient;
27
28
    /** @var MollieLoggerActionInterface */
29
    private $loggerAction;
30
31
    /** @var PaymentMethodRepositoryInterface */
32
    private $paymentMethodRepository;
33
34
    /** @var ChannelContextInterface */
35
    private $channelContext;
36
37
    public function __construct(
38
        MollieApiClient $mollieApiClient,
39
        MollieLoggerActionInterface $loggerAction,
40
        PaymentMethodRepositoryInterface $paymentMethodRepository,
41
        ChannelContextInterface $channelContext
42
    ) {
43
        $this->mollieApiClient = $mollieApiClient;
44
        $this->loggerAction = $loggerAction;
45
        $this->paymentMethodRepository = $paymentMethodRepository;
46
        $this->channelContext = $channelContext;
47
    }
48
49
    public function getClientWithKey(): MollieApiClient
50
    {
51
        $paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName(
52
            $this->channelContext->getChannel(),
53
            MollieGatewayFactory::FACTORY_NAME,
54
        );
55
56
        if (null === $paymentMethod) {
57
            throw new UpdateHandlingException(sprintf('No payment method found'));
58
        }
59
60
        $gateway = $paymentMethod->getGatewayConfig();
61
62
        $config = $gateway->getConfig();
63
64
        $environment = true === $config['environment'] ?
65
            MollieGatewayConfigurationType::API_KEY_LIVE :
66
            MollieGatewayConfigurationType::API_KEY_TEST;
67
68
        try {
69
            return $this->mollieApiClient->setApiKey($config[$environment]);
70
        } catch (ApiException $e) {
71
            $this->loggerAction->addNegativeLog(sprintf('API call failed: %s', $e->getMessage()));
72
73
            throw new UpdateHandlingException(sprintf('API call failed: %s', htmlspecialchars($e->getMessage())));
74
        }
75
    }
76
}
77