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

MolliePaymentsMethodResolver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 42
c 1
b 0
f 0
dl 0
loc 99
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A resolve() 0 27 5
A getDefaultOptions() 0 7 1
A getMolliePaymentOptions() 0 25 4
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\GatewayConfigInterface;
16
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
17
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
18
use Sylius\Component\Core\Model\OrderInterface;
19
use Sylius\Component\Order\Context\CartContextInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
use Symfony\Component\HttpFoundation\Session\Session;
22
23
final class MolliePaymentsMethodResolver implements MolliePaymentsMethodResolverInterface
24
{
25
    /** @var RepositoryInterface */
26
    private $orderRepository;
27
28
    /** @var RepositoryInterface */
29
    private $gatewayConfigRepository;
30
31
    /** @var RepositoryInterface */
32
    private $mollieGatewayRepository;
33
34
    /** @var Session */
35
    private $session;
36
37
    /** @var CartContextInterface */
38
    private $cartContext;
39
40
    /** @var MollieCountriesRestrictionResolverInterface */
41
    private $countriesRestrictionResolver;
42
43
    public function __construct(
44
        RepositoryInterface $orderRepository,
45
        RepositoryInterface $gatewayConfigRepository,
46
        RepositoryInterface $mollieGatewayRepository,
47
        Session $session,
48
        CartContextInterface $cartContext,
49
        MollieCountriesRestrictionResolverInterface $countriesRestrictionResolver
50
    ) {
51
        $this->orderRepository = $orderRepository;
52
        $this->gatewayConfigRepository = $gatewayConfigRepository;
53
        $this->mollieGatewayRepository = $mollieGatewayRepository;
54
        $this->session = $session;
55
        $this->cartContext = $cartContext;
56
        $this->countriesRestrictionResolver = $countriesRestrictionResolver;
57
    }
58
59
    public function resolve(): array
60
    {
61
        $orderId = $this->session->get('sylius_order_id');
62
        $orderFromSession = null;
63
64
        $order = $this->cartContext->getCart();
65
66
        if (null !== $orderId) {
67
            $orderFromSession = $this->orderRepository->findOneBy(['id' => $orderId]);
68
        }
69
70
        if (null !== $orderFromSession) {
71
            $order = $orderFromSession;
72
        }
73
74
        /** @var OrderInterface $order */
75
        $address = $order->getBillingAddress();
76
77
        if (null === $address) {
78
            $address = $order->getShippingAddress();
79
        }
80
81
        if (null === $address) {
82
            return $this->getDefaultOptions();
83
        }
84
85
        return $this->getMolliePaymentOptions($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

85
        return $this->getMolliePaymentOptions(/** @scrutinizer ignore-type */ $address->getCountryCode());
Loading history...
86
    }
87
88
    private function getMolliePaymentOptions(string $countryCode): array
89
    {
90
        $methods = $this->getDefaultOptions();
91
92
        /** @var GatewayConfigInterface $gateway */
93
        $gateway = $this->gatewayConfigRepository->findOneBy([
94
            'factoryName' => MollieGatewayFactory::FACTORY_NAME,
95
        ]);
96
97
        if (null === $gateway) {
98
            return $this->getDefaultOptions();
99
        }
100
101
        $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

101
        /** @scrutinizer ignore-call */ 
102
        $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...
102
103
        if (empty($paymentConfigs)) {
104
            return $this->getDefaultOptions();
105
        }
106
107
        /** @var MollieGatewayConfigInterface $paymentMethod */
108
        foreach ($paymentConfigs as $paymentMethod) {
109
            $methods = $this->countriesRestrictionResolver->resolve($paymentMethod, $methods, $countryCode);
110
        }
111
112
        return $methods;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $methods could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
113
    }
114
115
    private function getDefaultOptions(): array
116
    {
117
        return [
118
            'data' => [],
119
            'image' => [],
120
            'issuers' => [],
121
            'paymentFee' => [],
122
        ];
123
    }
124
}
125