Completed
Pull Request — master (#76)
by
unknown
10:41
created

PaymentMethodCheckoutValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A validate() 0 18 3
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\Validator\Constraints;
13
14
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
15
use Payum\Core\Model\GatewayConfigInterface;
16
use Sylius\Component\Core\Model\PaymentInterface;
17
use Sylius\Component\Order\Context\CartContextInterface;
18
use Symfony\Component\HttpFoundation\Session\Session;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\ConstraintValidator;
21
22
final class PaymentMethodCheckoutValidator extends ConstraintValidator
23
{
24
    /** @var CartContextInterface */
25
    private $cartContext;
26
27
    /** @var Session */
28
    private $session;
29
30
    public function __construct(
31
        CartContextInterface $cartContext,
32
        Session $session
33
    ) {
34
        $this->cartContext = $cartContext;
35
        $this->session = $session;
36
    }
37
38
    public function validate($value, Constraint $constraint)
39
    {
40
        $order = $this->cartContext->getCart();
41
42
        /** @var PaymentInterface $payment */
43
        $payment = $order->getPayments()->last();
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

43
        $payment = $order->/** @scrutinizer ignore-call */ getPayments()->last();
Loading history...
44
        /** @var GatewayConfigInterface  $gateway */
45
        $gateway = $payment->getMethod()->getGatewayConfig();
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

45
        $gateway = $payment->getMethod()->/** @scrutinizer ignore-call */ getGatewayConfig();
Loading history...
46
47
        if (MollieGatewayFactory::FACTORY_NAME !== $gateway->getFactoryName()) {
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

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

47
        if (MollieGatewayFactory::FACTORY_NAME !== /** @scrutinizer ignore-deprecated */ $gateway->getFactoryName()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
48
            return;
49
        }
50
        if (null !== $value) {
51
            return;
52
        }
53
54
        $this->session->getFlashBag()->add('error', 'bitbag_sylius_mollie_plugin.empty_payment_method_checkout');
55
        $this->context->buildViolation($constraint->message)->setTranslationDomain('messages')->addViolation();
56
    }
57
}
58