Passed
Pull Request — master (#133)
by
unknown
06:53
created

MolliePaymentsMethodResolver::requestMethods()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 25
rs 9.7666
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\Client\MollieApiClient;
16
use BitBag\SyliusMolliePlugin\Entity\GatewayConfigInterface;
17
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
18
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
19
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
20
use BitBag\SyliusMolliePlugin\Repository\PaymentMethodRepositoryInterface;
21
use BitBag\SyliusMolliePlugin\Resolver\Order\PaymentCheckoutOrderResolverInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Resource\Repository\RepositoryInterface;
24
25
final class MolliePaymentsMethodResolver implements MolliePaymentsMethodResolverInterface
26
{
27
    /** @var RepositoryInterface */
28
    private $mollieGatewayRepository;
29
30
    /** @var MollieCountriesRestrictionResolverInterface */
31
    private $countriesRestrictionResolver;
32
33
    /** @var ProductVoucherTypeCheckerInterface */
34
    private $productVoucherTypeChecker;
35
36
    /** @var PaymentCheckoutOrderResolverInterface */
37
    private $paymentCheckoutOrderResolver;
38
39
    /** @var PaymentMethodRepositoryInterface */
40
    private $paymentMethodRepository;
41
42
    /** @var MollieAmountRestrictionResolverInterface  */
43
    private $amountRestrictionResolver;
44
45
    /** @var RepositoryInterface  */
46
    private $gatewayConfigRepository;
47
48
    /** @var MollieApiClient  */
49
    private $mollieApiClient;
50
51
    public function __construct(
52
        RepositoryInterface $mollieGatewayRepository,
53
        MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver,
54
        ProductVoucherTypeCheckerInterface $productVoucherTypeChecker,
55
        PaymentCheckoutOrderResolverInterface $paymentCheckoutOrderResolver,
56
        PaymentMethodRepositoryInterface $paymentMethodRepository,
57
        MollieAmountRestrictionResolverInterface $amountRestrictionResolver,
58
        RepositoryInterface $gatewayConfigRepository,
59
        MollieApiClient $mollieApiClient
60
    ) {
61
        $this->mollieGatewayRepository = $mollieGatewayRepository;
62
        $this->countriesRestrictionResolver = $countriesRestrictionResolver;
63
        $this->productVoucherTypeChecker = $productVoucherTypeChecker;
64
        $this->paymentCheckoutOrderResolver = $paymentCheckoutOrderResolver;
65
        $this->paymentMethodRepository = $paymentMethodRepository;
66
        $this->amountRestrictionResolver = $amountRestrictionResolver;
67
        $this->gatewayConfigRepository = $gatewayConfigRepository;
68
        $this->mollieApiClient = $mollieApiClient;
69
    }
70
71
    public function resolve(): array
72
    {
73
        $order = $this->paymentCheckoutOrderResolver->resolve();
74
75
        /** @var OrderInterface $order */
76
        $address = $order->getBillingAddress();
77
78
        if (null === $address) {
79
            $address = $order->getShippingAddress();
80
        }
81
82
        if (null === $address) {
83
            return $this->getDefaultOptions();
84
        }
85
86
        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

86
        return $this->getMolliePaymentOptions($order, /** @scrutinizer ignore-type */ $address->getCountryCode());
Loading history...
87
    }
88
89
    private function getMolliePaymentOptions(OrderInterface $order, string $countryCode): array
90
    {
91
        $requestedMethods = $this->requestMethods($order);
0 ignored issues
show
Unused Code introduced by
The assignment to $requestedMethods is dead and can be removed.
Loading history...
92
93
        $methods = $this->getDefaultOptions();
94
        /** @var GatewayConfigInterface $gateway */
95
        $paymentMethod = $this->paymentMethodRepository->findOneByChannelAndGatewayFactoryName(
96
            $order->getChannel(),
97
            MollieGatewayFactory::FACTORY_NAME
98
        );
99
100
        if (null === $paymentMethod) {
101
            return $this->getDefaultOptions();
102
        }
103
104
        $gateway = $paymentMethod->getGatewayConfig();
105
106
        if (null === $gateway) {
107
            return $this->getDefaultOptions();
108
        }
109
110
        $paymentConfigs = $this->mollieGatewayRepository->findAllEnabledByGateway($gateway);
111
112
        if (empty($paymentConfigs)) {
113
            return $this->getDefaultOptions();
114
        }
115
116
        /** @var MollieGatewayConfigInterface $paymentMethod */
117
        foreach ($paymentConfigs as $paymentMethod) {
118
            $methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode);
119
        }
120
121
        foreach ($paymentConfigs as $paymentMethod) {
122
            $methods = $this->amountRestrictionResolver->resolve($paymentMethod, $methods, $order);
123
        }
124
125
        $methods = $this->productVoucherTypeChecker->checkTheProductTypeOnCart($order, $methods);
126
127
        return $methods;
128
    }
129
130
    private function getDefaultOptions(): array
131
    {
132
        return [
133
            'data' => [],
134
            'image' => [],
135
            'issuers' => [],
136
            'paymentFee' => [],
137
        ];
138
    }
139
140
    private function requestMethods(OrderInterface $order): array
141
    {
142
        $gateways = $this->gatewayConfigRepository->findBy(['factoryName' => MollieGatewayFactory::FACTORY_NAME]);
143
144
        /** @var GatewayConfigInterface $gateway */
145
        foreach ($gateways as $gateway) {
146
            $config = $gateway->getConfig();
147
            $environment = true === $config['environment'] ?
148
                MollieGatewayConfigurationType::API_KEY_LIVE :
149
                MollieGatewayConfigurationType::API_KEY_TEST;
150
151
            $client = $this->mollieApiClient->setApiKey($config[$environment]);
152
153
            $allMollieMethods = $client->methods->allActive([
154
                'amount[value]' => substr_replace((string) $order->getTotal(), '.', -2, 0),
155
                'amount[currency]' => $order->getCurrencyCode(),
156
                'locale' => $order->getLocaleCode()
157
            ]);
158
        }
159
160
        foreach($allMollieMethods as $method) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $allMollieMethods seems to be defined by a foreach iteration on line 145. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
161
            dump($method);
162
        }
163
        die;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return array. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
164
        return [];
0 ignored issues
show
Unused Code introduced by
return array() is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
165
    }
166
}
167