Passed
Pull Request — master (#78)
by
unknown
08:42
created

MollieMethodsCreator::createMethods()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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