Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

MethodsAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 33
c 1
b 0
f 0
dl 0
loc 72
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A __invoke() 0 36 5
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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Controller\Action\Admin;
14
15
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
16
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayConfigFactoryInterface;
18
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
19
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
20
use BitBag\SyliusMolliePlugin\Payments\Methods;
21
use Doctrine\ORM\EntityManagerInterface;
22
use Mollie\Api\Exceptions\ApiException;
23
use Sylius\Component\Resource\Exception\UpdateHandlingException;
24
use Sylius\Component\Resource\Repository\RepositoryInterface;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
28
final class MethodsAction
29
{
30
    /** @var MollieApiClient */
31
    private $mollieApiClient;
32
33
    /** @var RepositoryInterface */
34
    private $gatewayConfigRepository;
35
36
    /** @var Methods */
37
    private $methods;
38
39
    /** @var EntityManagerInterface */
40
    private $entityManager;
41
42
    /** @var MollieGatewayConfigFactoryInterface */
43
    private $factory;
44
45
    /** @var MollieLoggerActionInterface */
46
    private $loggerAction;
47
48
    public function __construct(
49
        MollieApiClient $mollieApiClient,
50
        RepositoryInterface $gatewayConfigRepository,
51
        Methods $methods,
52
        EntityManagerInterface $entityManager,
53
        MollieGatewayConfigFactoryInterface $factory,
54
        MollieLoggerActionInterface $loggerAction
55
    ) {
56
        $this->gatewayConfigRepository = $gatewayConfigRepository;
57
        $this->mollieApiClient = $mollieApiClient;
58
        $this->methods = $methods;
59
        $this->entityManager = $entityManager;
60
        $this->factory = $factory;
61
        $this->loggerAction = $loggerAction;
62
    }
63
64
    public function __invoke(Request $request): Response
65
    {
66
        $parameters = [
67
            'include' => 'issuers',
68
            'includeWallets' => 'applepay',
69
        ];
70
71
        /** @var GatewayConfigInterface $gateway */
72
        $gateway = $this->gatewayConfigRepository->findOneBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
73
74
        $config = $gateway->getConfig();
75
        $environment = true === $config['environment'] ? 'api_key_live' : 'api_key_test';
76
77
        try {
78
            $client = $this->mollieApiClient->setApiKey($config[$environment]);
79
            $allMollieMethods = $client->methods->allActive($parameters);
80
        } catch (ApiException $e) {
81
            $this->loggerAction->addNegativeLog(sprintf('API call failed: %s', $e->getMessage()));
82
83
            throw new UpdateHandlingException(sprintf('API call failed: %s', htmlspecialchars($e->getMessage())));
84
        }
85
86
        foreach ($allMollieMethods as $mollieMethod) {
87
            $this->methods->add($mollieMethod);
88
        }
89
90
        foreach ($this->methods->getAllEnabled() as $method) {
91
            $gatewayConfig = $this->factory->create($method, $gateway);
92
93
            $this->entityManager->persist($gatewayConfig);
94
            $this->entityManager->flush();
95
        }
96
97
        $this->loggerAction->addLog(sprintf('Downloaded all methods from mollie API'));
98
99
        return new Response('OK', Response::HTTP_OK);
100
    }
101
}
102