Completed
Pull Request — master (#49)
by
unknown
06:22
created

MollieCountriesRestrictionResolver::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 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
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusMolliePlugin\Resolver;
14
15
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
16
17
final class MollieCountriesRestrictionResolver implements MollieCountriesRestrictionResolverInterface
18
{
19
    /** @var MolliePaymentMethodImageResolverInterface */
20
    private $imageResolver;
21
22
    public function __construct(MolliePaymentMethodImageResolverInterface $imageResolver)
23
    {
24
        $this->imageResolver = $imageResolver;
25
    }
26
27
    public function resolve(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): ?array
28
    {
29
        if ($paymentMethod->getCountryRestriction() === MollieGatewayConfigInterface::ALL_COUNTRIES) {
30
            return $this->excludeCountryLevel($paymentMethod, $methods, $countryCode);
31
        }
32
        if ($paymentMethod->getCountryRestriction() === MollieGatewayConfigInterface::SELECTED_COUNTRIES) {
33
            return $this->allowCountryLevel($paymentMethod, $methods, $countryCode);
34
        }
35
36
        return $methods;
37
    }
38
39
    private function allowCountryLevel(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): array
40
    {
41
        if (is_array($paymentMethod->getCountryLevelAllowed()) &&
42
            in_array($countryCode, $paymentMethod->getCountryLevelAllowed())) {
43
            return $this->setData($methods, $paymentMethod);
44
        }
45
46
        return $methods;
47
    }
48
49
    private function excludeCountryLevel(MollieGatewayConfigInterface $paymentMethod, array $methods, string $countryCode): array
50
    {
51
        if (is_array($paymentMethod->getCountryLevelExcluded()) &&
52
            in_array($countryCode, $paymentMethod->getCountryLevelExcluded())) {
53
            return $methods;
54
        }
55
56
        return $this->setData($methods, $paymentMethod);
57
    }
58
59
    private function setData(array $methods, MollieGatewayConfigInterface $paymentMethod): array
60
    {
61
        $methods['data'][$paymentMethod->getName()] = $paymentMethod->getMethodId();
0 ignored issues
show
Bug introduced by
The method getMethodId() does not exist on BitBag\SyliusMolliePlugi...eGatewayConfigInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to BitBag\SyliusMolliePlugi...eGatewayConfigInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        /** @scrutinizer ignore-call */ 
62
        $methods['data'][$paymentMethod->getName()] = $paymentMethod->getMethodId();
Loading history...
62
        $methods['image'][$paymentMethod->getMethodId()] = $this->imageResolver->resolve($paymentMethod);
63
        $methods['issuers'][$paymentMethod->getMethodId()] = $paymentMethod->getIssuers();
64
        $methods['paymentFee'][$paymentMethod->getMethodId()] = $paymentMethod->getPaymentSurchargeFee()->getType()
65
            ? $paymentMethod->getPaymentSurchargeFee() : [];
66
67
        return $methods;
68
    }
69
}
70