Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

getMolliePaymentOptions()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 28
rs 9.2222
cc 6
nc 5
nop 1
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
63
        $order = $this->cartContext->getCart();
64
65
        if (null !== $orderId) {
66
            $order = $this->orderRepository->findOneBy(['id' => $orderId]);
67
        }
68
69
        /** @var OrderInterface $order */
70
        $address = $order->getBillingAddress();
71
72
        if (null === $address) {
73
            $address = $order->getShippingAddress();
74
        }
75
76
        if (null === $address) {
77
            return $this->getDefaultOptions();
78
        }
79
80
        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

80
        return $this->getMolliePaymentOptions(/** @scrutinizer ignore-type */ $address->getCountryCode());
Loading history...
81
    }
82
83
    private function getMolliePaymentOptions(string $countryCode): array
84
    {
85
        $methods = $this->getDefaultOptions();
86
87
        /** @var GatewayConfigInterface $gateway */
88
        $gateway = $this->gatewayConfigRepository->findOneBy([
89
            'factoryName' => MollieGatewayFactory::FACTORY_NAME,
90
        ]);
91
92
        $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

92
        /** @scrutinizer ignore-call */ 
93
        $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...
93
94
        if (empty($paymentConfigs)) {
95
            return $this->getDefaultOptions();
96
        }
97
98
        /** @var MollieGatewayConfigInterface $paymentMethod */
99
        foreach ($paymentConfigs as $paymentMethod) {
100
101
            if (in_array($countryCode, $paymentMethod->getCountryLevel()) || empty($paymentMethod->getCountryLevel())) {
102
                $methods['data'][$paymentMethod->getName()] = $paymentMethod->getMethodId();
103
                $methods['image'][$paymentMethod->getMethodId()] = $this->imageResolver->resolve($paymentMethod);
104
                $methods['issuers'][$paymentMethod->getMethodId()] = $paymentMethod->getIssuers();
105
                $methods['paymentFee'][$paymentMethod->getMethodId()] = $paymentMethod->getPaymentSurchargeFee()->getType()
106
                    ? $paymentMethod->getPaymentSurchargeFee(): [];
107
            }
108
        }
109
110
        return $methods;
111
    }
112
113
    private function getDefaultOptions(): array
114
    {
115
        return [
116
            'data' => [],
117
            'image' => [],
118
            'issuers' => [],
119
            'paymentFee' => [],
120
        ];
121
    }
122
}
123