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

PaymentSurchargeProcessor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 49
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMolliePaymentSurcharge() 0 11 3
A process() 0 22 3
A __construct() 0 4 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\Processor;
14
15
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfig;
16
use BitBag\SyliusMolliePlugin\PaymentFee\Calculate;
17
use Payum\Core\Model\PaymentInterface;
18
use Sylius\Component\Order\Model\OrderInterface;
19
use Sylius\Component\Payment\Model\PaymentMethodInterface;
20
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21
22
final class PaymentSurchargeProcessor implements PaymentSurchargeProcessorInterface
23
{
24
    /** @var Calculate */
25
    private $calculate;
26
27
    /** @var SessionInterface */
28
    private $session;
29
30
    public function __construct(Calculate $calculate, SessionInterface $session)
31
    {
32
        $this->calculate = $calculate;
33
        $this->session = $session;
34
    }
35
36
    public function process(OrderInterface $order): void
37
    {
38
        /** @var PaymentInterface $payment */
39
        $payment = $order->getPayments()->first();
0 ignored issues
show
Bug introduced by
The method getPayments() does not exist on Sylius\Component\Order\Model\OrderInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Order\Model\Order. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
        $payment = $order->/** @scrutinizer ignore-call */ getPayments()->first();
Loading history...
40
41
        /** @var PaymentMethodInterface $paymentMethod */
42
        $paymentMethod = $payment->getMethod();
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Payum\Core\Model\PaymentInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        /** @scrutinizer ignore-call */ 
43
        $paymentMethod = $payment->getMethod();

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...
43
44
        if ($paymentMethod->getGatewayConfig()->getFactoryName() !== 'mollie') {
0 ignored issues
show
Bug introduced by
The method getGatewayConfig() does not exist on Sylius\Component\Payment...\PaymentMethodInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Payment\Model\PaymentMethod. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        if ($paymentMethod->/** @scrutinizer ignore-call */ getGatewayConfig()->getFactoryName() !== 'mollie') {
Loading history...
45
            return;
46
        }
47
48
        $data = $this->session->get('mollie_payment_options', null);
49
        $molliePaymentMethod = $data['molliePaymentMethods'];
50
51
        $paymentSurcharge = $this->getMolliePaymentSurcharge($paymentMethod, $molliePaymentMethod);
52
53
        if (null === $paymentSurcharge) {
54
            return;
55
        }
56
57
        $this->calculate->calculateFromCart($order, $paymentSurcharge);
58
    }
59
60
    private function getMolliePaymentSurcharge(PaymentMethodInterface $paymentMethod, string $molliePaymentMethod): ?MollieGatewayConfig
61
    {
62
        $configMethods = $paymentMethod->getGatewayConfig()->getMollieGatewayConfig();
63
        foreach ($configMethods as $configMethod) {
64
            /** @var MollieGatewayConfig $configMethod */
65
            if ($configMethod->getMethodId() === $molliePaymentMethod) {
66
                return $configMethod;
67
            }
68
        }
69
70
        return null;
71
    }
72
}
73