OrderAmountValidator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 42
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 34 6
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\SyliusQuadPayPlugin\Validator\Constraints;
14
15
use BitBag\SyliusQuadPayPlugin\QuadPayGatewayFactory;
16
use Sylius\Component\Core\Model\PaymentInterface;
17
use Sylius\Component\Core\Model\PaymentMethodInterface;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\ConstraintValidator;
20
use Webmozart\Assert\Assert;
21
22
final class OrderAmountValidator extends ConstraintValidator
23
{
24
    /**
25
     * @param PaymentInterface $payment
26
     * @param Constraint|OrderAmount $constraint
27
     *
28
     * {@inheritdoc}
29
     */
30
    public function validate($payment, Constraint $constraint): void
31
    {
32
        Assert::isInstanceOf($payment, PaymentInterface::class);
33
34
        Assert::isInstanceOf($constraint, OrderAmount::class);
35
36
        /** @var PaymentMethodInterface $paymentMethod */
37
        $paymentMethod = $payment->getMethod();
38
39
        if (null === $paymentMethod) {
40
            return;
41
        }
42
43
        $gatewayConfig = $paymentMethod->getGatewayConfig();
44
45
        if (null === $gatewayConfig || ($gatewayConfig->getFactoryName() !== QuadPayGatewayFactory::FACTORY_NAME)) {
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

45
        if (null === $gatewayConfig || (/** @scrutinizer ignore-deprecated */ $gatewayConfig->getFactoryName() !== QuadPayGatewayFactory::FACTORY_NAME)) {

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...
46
            return;
47
        }
48
49
        $config = $gatewayConfig->getConfig();
50
51
        $minimumAmount = $config['minimumAmount'];
52
        $maximumAmount = $config['maximumAmount'];
53
54
        if ($minimumAmount > $payment->getAmount()) {
55
            $this->context->buildViolation($constraint->minimumAmountMessage, [
56
                '{{ minimumAmount }}' => number_format(abs($minimumAmount / 100), 2, '.', ','),
57
            ])->atPath('method')->addViolation();
58
        }
59
60
        if ($maximumAmount < $payment->getAmount()) {
61
            $this->context->buildViolation($constraint->maximumAmountMessage, [
62
                '{{ maximumAmount }}' => number_format(abs($maximumAmount / 100), 2, '.', ','),
63
            ])->atPath('method')->addViolation();
64
        }
65
    }
66
}
67