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

MolliePaymentsMethodResolver::getDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 7
rs 10
cc 1
nc 1
nop 0
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
50
    public function __construct(
51
        RepositoryInterface $mollieGatewayRepository,
52
        MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver,
53
        ProductVoucherTypeCheckerInterface $productVoucherTypeChecker,
54
        PaymentCheckoutOrderResolverInterface $paymentCheckoutOrderResolver,
55
        PaymentMethodRepositoryInterface $paymentMethodRepository,
56
        MollieAllowedMethodsResolver $allowedMethodsResolver,
57
        MollieLoggerActionInterface $loggerAction
58
    ) {
59
        $this->mollieGatewayRepository = $mollieGatewayRepository;
60
        $this->countriesRestrictionResolver = $countriesRestrictionResolver;
61
        $this->productVoucherTypeChecker = $productVoucherTypeChecker;
62
        $this->paymentCheckoutOrderResolver = $paymentCheckoutOrderResolver;
63
        $this->paymentMethodRepository = $paymentMethodRepository;
64
        $this->allowedMethodsResolver = $allowedMethodsResolver;
65
        $this->loggerAction = $loggerAction;
66
    }
67
68
    public function resolve(): array
69
    {
70
        $order = $this->paymentCheckoutOrderResolver->resolve();
71
72
        /** @var OrderInterface $order */
73
        $address = $order->getBillingAddress();
74
75
        if (null === $address) {
76
            $address = $order->getShippingAddress();
77
        }
78
79
        if (null === $address) {
80
            return $this->getDefaultOptions();
81
        }
82
83
        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

83
        return $this->getMolliePaymentOptions($order, /** @scrutinizer ignore-type */ $address->getCountryCode());
Loading history...
84
    }
85
86
    private function getMolliePaymentOptions(OrderInterface $order, string $countryCode): array
87
    {
88
        $allowedMethods = [];
89
90
        $methods = $this->getDefaultOptions();
91
92
        /** @var GatewayConfigInterface $gateway */
93
        $paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName(
94
            $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

94
            /** @scrutinizer ignore-type */ $order->getChannel(),
Loading history...
95
            MollieGatewayFactory::FACTORY_NAME
96
        );
97
98
        if (null === $paymentMethod) {
99
            return $this->getDefaultOptions();
100
        }
101
102
        $gateway = $paymentMethod->getGatewayConfig();
103
104
        if (null === $gateway) {
105
            return $this->getDefaultOptions();
106
        }
107
108
        $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

108
        /** @scrutinizer ignore-call */ 
109
        $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...
109
110
        if (empty($paymentConfigs)) {
111
            return $this->getDefaultOptions();
112
        }
113
114
        try {
115
            $allowedMethodsIds = $this->allowedMethodsResolver->resolve($order);
116
        } catch (ApiException $e) {
117
            $this->loggerAction->addNegativeLog($e->getMessage());
118
119
            return $this->getDefaultOptions();
120
        }
121
122
        /** @var MollieGatewayConfig $paymentMethod */
123
        foreach ($paymentConfigs as $paymentMethod) {
124
            if (in_array($paymentMethod->getMethodId(), $allowedMethodsIds, true)) {
125
                $allowedMethods[] = $paymentMethod;
126
            }
127
        }
128
129
        if (empty($allowedMethods)) {
130
            return $this->getDefaultOptions();
131
        }
132
133
        /** @var MollieGatewayConfigInterface $paymentMethod */
134
        foreach ($allowedMethods as $paymentMethod) {
135
            $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

135
            $methods = $this->countriesRestrictionResolver->resolve($paymentMethod, /** @scrutinizer ignore-type */ $methods, $countryCode);
Loading history...
136
        }
137
138
        $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

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