MollieCountriesRestrictionResolver   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 1
b 0
f 0
dl 0
loc 51
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setData() 0 9 2
A resolve() 0 10 3
A allowCountryLevel() 0 8 3
A excludeCountryLevel() 0 8 3
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\Entity\MollieGatewayConfigInterface;
15
16
final class MollieCountriesRestrictionResolver implements MollieCountriesRestrictionResolverInterface
17
{
18
    /** @var MolliePaymentMethodImageResolverInterface */
19
    private $imageResolver;
20
21
    public function __construct(MolliePaymentMethodImageResolverInterface $imageResolver)
22
    {
23
        $this->imageResolver = $imageResolver;
24
    }
25
26
    public function resolve(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): ?array
27
    {
28
        if ($paymentMethod->getCountryRestriction() === MollieGatewayConfigInterface::ALL_COUNTRIES) {
29
            return $this->excludeCountryLevel($paymentMethod, $methods, $countryCode);
30
        }
31
        if ($paymentMethod->getCountryRestriction() === MollieGatewayConfigInterface::SELECTED_COUNTRIES) {
32
            return $this->allowCountryLevel($paymentMethod, $methods, $countryCode);
33
        }
34
35
        return $methods;
36
    }
37
38
    private function allowCountryLevel(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): array
39
    {
40
        if (is_array($paymentMethod->getCountryLevelAllowed()) &&
41
            in_array($countryCode, $paymentMethod->getCountryLevelAllowed())) {
42
            return $this->setData($methods, $paymentMethod);
43
        }
44
45
        return $methods;
46
    }
47
48
    private function excludeCountryLevel(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): array
49
    {
50
        if (is_array($paymentMethod->getCountryLevelExcluded()) &&
51
            in_array($countryCode, $paymentMethod->getCountryLevelExcluded())) {
52
            return $methods;
53
        }
54
55
        return $this->setData($methods, $paymentMethod);
56
    }
57
58
    private function setData(array $methods, MollieGatewayConfigInterface $paymentMethod): array
59
    {
60
        $methods['data'][$paymentMethod->getName()] = $paymentMethod->getMethodId();
61
        $methods['image'][$paymentMethod->getMethodId()] = $this->imageResolver->resolve($paymentMethod);
62
        $methods['issuers'][$paymentMethod->getMethodId()] = $paymentMethod->getIssuers();
63
        $methods['paymentFee'][$paymentMethod->getMethodId()] = $paymentMethod->getPaymentSurchargeFee()->getType()
64
            ? $paymentMethod->getPaymentSurchargeFee() : [];
65
66
        return $methods;
67
    }
68
}
69