Passed
Push — master ( 5cf396...db4681 )
by
unknown
11:56 queued 04:44
created

MollieMethodsCreator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 16 2
A __construct() 0 14 1
A createMethods() 0 16 4
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\Entity\GatewayConfigInterface;
16
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayConfigFactoryInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
18
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
19
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
20
use BitBag\SyliusMolliePlugin\Payments\Methods;
21
use BitBag\SyliusMolliePlugin\Payments\Methods\MethodInterface;
22
use Doctrine\ORM\EntityManagerInterface;
23
use Mollie\Api\Resources\MethodCollection;
24
use Sylius\Component\Resource\Repository\RepositoryInterface;
25
26
final class MollieMethodsCreator implements MollieMethodsCreatorInterface
27
{
28
    /** @var Methods */
29
    private $methods;
30
31
    /** @var EntityManagerInterface */
32
    private $entityManager;
33
34
    /** @var MollieLoggerActionInterface */
35
    private $loggerAction;
36
37
    /** @var MollieApiClient */
38
    private $mollieApiClient;
39
40
    /** @var RepositoryInterface */
41
    private $gatewayConfigRepository;
42
43
    /** @var MollieGatewayConfigFactoryInterface */
44
    private $factory;
45
46
    public function __construct(
47
        Methods $methods,
48
        EntityManagerInterface $entityManager,
49
        MollieLoggerActionInterface $loggerAction,
50
        MollieApiClient $mollieApiClient,
51
        RepositoryInterface $gatewayConfigRepository,
52
        MollieGatewayConfigFactoryInterface $factory
53
    ) {
54
        $this->methods = $methods;
55
        $this->entityManager = $entityManager;
56
        $this->loggerAction = $loggerAction;
57
        $this->mollieApiClient = $mollieApiClient;
58
        $this->gatewayConfigRepository = $gatewayConfigRepository;
59
        $this->factory = $factory;
60
    }
61
62
    public function create(): void
63
    {
64
        /** @var GatewayConfigInterface $gateway */
65
        $gateway = $this->gatewayConfigRepository->findOneBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
66
67
        $config = $gateway->getConfig();
68
        $environment = true === $config['environment'] ?
69
            MollieGatewayConfigurationType::API_KEY_LIVE :
70
            MollieGatewayConfigurationType::API_KEY_TEST;
71
72
        $client = $this->mollieApiClient->setApiKey($config[$environment]);
73
74
        $allMollieMethods = $client->methods->allActive(self::PARAMETERS);
75
        $this->createMethods($allMollieMethods, $gateway);
76
77
        $this->loggerAction->addLog(sprintf('Downloaded all methods from mollie API'));
78
    }
79
80
    private function createMethods(MethodCollection $allMollieMethods, GatewayConfigInterface $gateway): void
81
    {
82
        foreach ($allMollieMethods as $mollieMethod) {
83
            if (in_array($mollieMethod->id, self::UNSUPPORTED_METHODS)) {
84
                continue;
85
            }
86
87
            $this->methods->add($mollieMethod);
88
        }
89
90
        /** @var MethodInterface $method */
91
        foreach ($this->methods->getAllEnabled() as $method) {
92
            $gatewayConfig = $this->factory->create($method, $gateway);
93
94
            $this->entityManager->persist($gatewayConfig);
95
            $this->entityManager->flush();
96
        }
97
    }
98
}
99