Passed
Push — master ( bcf326...7e4b19 )
by
unknown
05:14
created

MolliePaymentsMethodResolver::resolve()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 27
rs 9.5222
cc 5
nc 16
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
 * 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 MolliePaymentMethodImageResolverInterface */
32
    private $imageResolver;
33
34
    /** @var RepositoryInterface */
35
    private $mollieGatewayRepository;
36
37
    /** @var Session */
38
    private $session;
39
40
    /** @var CartContextInterface */
41
    private $cartContext;
42
43
    public function __construct(
44
        RepositoryInterface $orderRepository,
45
        RepositoryInterface $gatewayConfigRepository,
46
        MolliePaymentMethodImageResolverInterface $imageResolver,
47
        RepositoryInterface $mollieGatewayRepository,
48
        Session $session,
49
        CartContextInterface $cartContext
50
    ) {
51
        $this->orderRepository = $orderRepository;
52
        $this->gatewayConfigRepository = $gatewayConfigRepository;
53
        $this->imageResolver = $imageResolver;
54
        $this->mollieGatewayRepository = $mollieGatewayRepository;
55
        $this->session = $session;
56
        $this->cartContext = $cartContext;
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
110
            if (in_array($countryCode, $paymentMethod->getCountryLevel()) || empty($paymentMethod->getCountryLevel())) {
111
                $methods['data'][$paymentMethod->getName()] = $paymentMethod->getMethodId();
112
                $methods['image'][$paymentMethod->getMethodId()] = $this->imageResolver->resolve($paymentMethod);
113
                $methods['issuers'][$paymentMethod->getMethodId()] = $paymentMethod->getIssuers();
114
                $methods['paymentFee'][$paymentMethod->getMethodId()] = $paymentMethod->getPaymentSurchargeFee()->getType()
115
                    ? $paymentMethod->getPaymentSurchargeFee(): [];
116
            }
117
        }
118
119
        return $methods;
120
    }
121
122
    private function getDefaultOptions(): array
123
    {
124
        return [
125
            'data' => [],
126
            'image' => [],
127
            'issuers' => [],
128
            'paymentFee' => [],
129
        ];
130
    }
131
}
132