Passed
Pull Request — master (#133)
by
unknown
07:02
created

MollieAllowedMethodsResolver::parseTotalToString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Entity\GatewayConfigInterface;
16
use Sylius\Component\Core\Model\OrderInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
18
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
19
use Mollie\Api\Exceptions\ApiException;
20
use Mollie\Api\Resources\Method;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
23
final class MollieAllowedMethodsResolver implements MollieAllowedMethodsResolverInterface
24
{
25
26
    /** @var RepositoryInterface */
27
    private $gatewayConfigRepository;
28
29
    /** @var MollieApiClient  */
30
    private $mollieApiClient;
31
32
    public function __construct(RepositoryInterface $gatewayConfigRepository, MollieApiClient $mollieApiClient)
33
    {
34
        $this->gatewayConfigRepository = $gatewayConfigRepository;
35
        $this->mollieApiClient = $mollieApiClient;
36
    }
37
38
    /**
39
     * @param OrderInterface $order
40
     * @return string[]
41
     * @throws ApiException
42
     */
43
    public function resolve(OrderInterface $order): array
44
    {
45
        $allowedMethods = [];
46
        $allowedMethodsIds = [];
47
48
        $gateways = $this->gatewayConfigRepository->findBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
49
50
        if (empty($gateways)) {
51
            return [];
52
        }
53
54
        /** @var GatewayConfigInterface $gateway */
55
        foreach ($gateways as $gateway) {
56
            $config = $gateway->getConfig();
57
            $environment = true === $config['environment'] ?
58
                MollieGatewayConfigurationType::API_KEY_LIVE :
59
                MollieGatewayConfigurationType::API_KEY_TEST;
60
61
            $client = $this->mollieApiClient->setApiKey($config[$environment]);
62
63
            $allowedMethods = $client->methods->allActive($this->createParametersByOrder($order));
64
        }
65
66
        /** @var Method $method */
67
        foreach ($allowedMethods as $method) {
68
            $allowedMethodsIds[] = $method->id;
69
        }
70
71
        return $allowedMethodsIds;
72
    }
73
74
    private function createParametersByOrder($order): array
75
    {
76
        return [
77
            'amount[value]' => $this->parseTotalToString($order->getTotal()),
78
            'amount[currency]' => $order->getCurrencyCode(),
79
            'locale' => $order->getLocaleCode(),
80
            'billingCountry' => null !== $order->getBillingAddress()
81
                ? $order->getBillingAddress()->getCountryCode()
82
                : null
83
        ];
84
    }
85
86
    private function parseTotalToString(int $total): string
87
    {
88
        return substr_replace((string) $total, '.', -2, 0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return substr_replace((string)$total, '.', -2, 0) could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
89
    }
90
}
91