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

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