Passed
Pull Request — master (#133)
by
unknown
09:36 queued 01:35
created

MollieAllowedMethodsResolver   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 45
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parseTotalToString() 0 3 1
A __construct() 0 3 1
A createParametersByOrder() 0 12 2
A resolve() 0 15 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\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[value]' => $this->parseTotalToString($order->getTotal()),
50
                'amount[currency]' => $order->getCurrencyCode(),
51
                'locale' => $order->getLocaleCode(),
52
                'billingCountry' => null !== $order->getBillingAddress()
53
                    ? $order->getBillingAddress()->getCountryCode()
54
                    : null
55
            ],
56
            MollieMethodsCreatorInterface::PARAMETERS
57
        );
58
    }
59
60
    private function parseTotalToString(int $total): string
61
    {
62
        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...
63
    }
64
}
65