Passed
Push — master ( 439abb...cbcfda )
by
unknown
09:10
created

createParametersByOrder()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.9666
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\Creator\MollieMethodsCreatorInterface;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Mollie\Api\Resources\Method;
17
18
final class MollieAllowedMethodsResolver implements MollieAllowedMethodsResolverInterface
19
{
20
    /** @var MollieApiClientKeyResolverInterface */
21
    private $mollieApiClientKeyResolver;
22
23
    public function __construct(MollieApiClientKeyResolverInterface $mollieApiClientKeyResolver)
24
    {
25
        $this->mollieApiClientKeyResolver = $mollieApiClientKeyResolver;
26
    }
27
28
    public function resolve(OrderInterface $order): array
29
    {
30
        $allowedMethodsIds = [];
31
32
        $client = $this->mollieApiClientKeyResolver->getClientWithKey();
33
34
        /** API will return only payment methods allowed for order total, currency, billing country */
35
        $allowedMethods = $client->methods->allActive($this->createParametersByOrder($order));
36
37
        /** @var Method $method */
38
        foreach ($allowedMethods as $method) {
39
            $allowedMethodsIds[] = $method->id;
40
        }
41
42
        return $allowedMethodsIds;
43
    }
44
45
    private function createParametersByOrder($order): array
46
    {
47
        return array_merge(
48
            [
49
                'amount' => [
50
                    'value' => $this->parseTotalToString($order->getTotal()),
51
                    'currency' => $order->getCurrencyCode(),
52
                ],
53
                'locale' => $order->getLocaleCode(),
54
                'billingCountry' => null !== $order->getBillingAddress()
55
                    ? $order->getBillingAddress()->getCountryCode()
56
                    : null
57
            ],
58
            MollieMethodsCreatorInterface::PARAMETERS
59
        );
60
    }
61
62
    private function parseTotalToString(int $total): string
63
    {
64
        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...
65
    }
66
}
67