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

MolliePaymentsMethodResolver   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 54
c 2
b 0
f 0
dl 0
loc 123
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getDefaultOptions() 0 7 1
B getMolliePaymentOptions() 0 55 9
A resolve() 0 16 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\Checker\Voucher\ProductVoucherTypeCheckerInterface;
15
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
16
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
17
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
18
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
19
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
20
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface;
21
use BitBag\SyliusMolliePlugin\Resolver\Order\PaymentCheckoutOrderResolverInterface;
22
use Mollie\Api\Exceptions\ApiException;
23
use Sylius\Component\Core\Model\OrderInterface;
24
use Sylius\Component\Resource\Repository\RepositoryInterface;
25
26
final class MolliePaymentsMethodResolver implements MolliePaymentsMethodResolverInterface
27
{
28
    /** @var RepositoryInterface */
29
    private $mollieGatewayRepository;
30
31
    /** @var MollieCountriesRestrictionResolverInterface */
32
    private $countriesRestrictionResolver;
33
34
    /** @var ProductVoucherTypeCheckerInterface */
35
    private $productVoucherTypeChecker;
36
37
    /** @var PaymentCheckoutOrderResolverInterface */
38
    private $paymentCheckoutOrderResolver;
39
40
    /** @var PaymentMethodRepositoryInterface */
41
    private $paymentMethodRepository;
42
43
    /** @var MollieAllowedMethodsResolver */
44
    private $allowedMethodsResolver;
45
46
    /** @var MollieLoggerActionInterface  */
47
    private $loggerAction;
48
49
    public function __construct(
50
        RepositoryInterface $mollieGatewayRepository,
51
        MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver,
52
        ProductVoucherTypeCheckerInterface $productVoucherTypeChecker,
53
        PaymentCheckoutOrderResolverInterface $paymentCheckoutOrderResolver,
54
        PaymentMethodRepositoryInterface $paymentMethodRepository,
55
        MollieAllowedMethodsResolver $allowedMethodsResolver,
56
        MollieLoggerActionInterface $loggerAction
57
    ) {
58
        $this->mollieGatewayRepository = $mollieGatewayRepository;
59
        $this->countriesRestrictionResolver = $countriesRestrictionResolver;
60
        $this->productVoucherTypeChecker = $productVoucherTypeChecker;
61
        $this->paymentCheckoutOrderResolver = $paymentCheckoutOrderResolver;
62
        $this->paymentMethodRepository = $paymentMethodRepository;
63
        $this->allowedMethodsResolver = $allowedMethodsResolver;
64
        $this->loggerAction = $loggerAction;
65
    }
66
67
    public function resolve(): array
68
    {
69
        $order = $this->paymentCheckoutOrderResolver->resolve();
70
71
        /** @var OrderInterface $order */
72
        $address = $order->getBillingAddress();
73
74
        if (null === $address) {
75
            $address = $order->getShippingAddress();
76
        }
77
78
        if (null === $address) {
79
            return $this->getDefaultOptions();
80
        }
81
82
        return $this->getMolliePaymentOptions($order, $address->getCountryCode());
0 ignored issues
show
Bug introduced by
It seems like $address->getCountryCode() can also be of type null; however, parameter $countryCode of BitBag\SyliusMolliePlugi...tMolliePaymentOptions() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

82
        return $this->getMolliePaymentOptions($order, /** @scrutinizer ignore-type */ $address->getCountryCode());
Loading history...
83
    }
84
85
    private function getMolliePaymentOptions(OrderInterface $order, string $countryCode): array
86
    {
87
        $allowedMethods = [];
88
89
        $methods = $this->getDefaultOptions();
90
91
        /** @var GatewayConfigInterface $gateway */
92
        $paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName(
93
            $order->getChannel(),
0 ignored issues
show
Bug introduced by
It seems like $order->getChannel() can also be of type null; however, parameter $channel of BitBag\SyliusMolliePlugi...AndGatewayFactoryName() does only seem to accept Sylius\Component\Core\Model\ChannelInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

93
            /** @scrutinizer ignore-type */ $order->getChannel(),
Loading history...
94
            MollieGatewayFactory::FACTORY_NAME
95
        );
96
97
        if (null === $paymentMethod) {
98
            return $this->getDefaultOptions();
99
        }
100
101
        $gateway = $paymentMethod->getGatewayConfig();
102
103
        if (null === $gateway) {
104
            return $this->getDefaultOptions();
105
        }
106
107
        $paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway);
0 ignored issues
show
Bug introduced by
The method findAllEnabledByGateway() does not exist on Sylius\Component\Resourc...ory\RepositoryInterface. Did you maybe mean findAll()? ( Ignorable by Annotation )

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

107
        /** @scrutinizer ignore-call */ 
108
        $paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
109
        if (empty($paymentConfigs)) {
110
            return $this->getDefaultOptions();
111
        }
112
113
        try {
114
            $allowedMethodsIds = $this->allowedMethodsResolver->resolve($order);
115
        } catch (ApiException $e) {
116
            $this->loggerAction->addNegativeLog($e->getMessage());
117
118
            return $this->getDefaultOptions();
119
        }
120
121
        /** @var MollieGatewayConfig $paymentMethod */
122
        foreach ($paymentConfigs as $paymentMethod) {
123
            if (in_array($paymentMethod->getMethodId(), $allowedMethodsIds, true)) {
124
                $allowedMethods[] = $paymentMethod;
125
            }
126
        }
127
128
        if (empty($allowedMethods)) {
129
            return $this->getDefaultOptions();
130
        }
131
132
        /** @var MollieGatewayConfigInterface $paymentMethod */
133
        foreach ($allowedMethods as $paymentMethod) {
134
            $methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode);
0 ignored issues
show
Bug introduced by
It seems like $methods can also be of type null; however, parameter $methods of BitBag\SyliusMolliePlugi...verInterface::resolve() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

134
            $methods = $this->countriesRestrictionResolver->resolve($paymentMethod, /** @scrutinizer ignore-type */ $methods, $countryCode);
Loading history...
135
        }
136
137
        $methods = $this->productVoucherTypeChecker->checkTheProductTypeOnCart($order, $methods);
0 ignored issues
show
Bug introduced by
It seems like $methods can also be of type null; however, parameter $methods of BitBag\SyliusMolliePlugi...kTheProductTypeOnCart() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

137
        $methods = $this->productVoucherTypeChecker->checkTheProductTypeOnCart($order, /** @scrutinizer ignore-type */ $methods);
Loading history...
138
139
        return $methods;
140
    }
141
142
    private function getDefaultOptions(): array
143
    {
144
        return [
145
            'data' => [],
146
            'image' => [],
147
            'issuers' => [],
148
            'paymentFee' => [],
149
        ];
150
    }
151
}
152