Completed
Push — currency-cookie ( 8ef560...28b724 )
by Kamil
24:47
created

OrderShippingMethodEligibilityValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 18 4
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\CoreBundle\Validator\Constraints;
13
14
use Sylius\Component\Core\Model\OrderInterface;
15
use Sylius\Component\Shipping\Checker\ShippingMethodEligibilityCheckerInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Webmozart\Assert\Assert;
19
20
/**
21
 * @author Arkadiusz Krakowiak <[email protected]>
22
 */
23
final class OrderShippingMethodEligibilityValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var ShippingMethodEligibilityCheckerInterface
27
     */
28
    private $methodEligibilityChecker;
29
30
    /**
31
     * @param ShippingMethodEligibilityCheckerInterface $methodEligibilityChecker
32
     */
33
    public function __construct(ShippingMethodEligibilityCheckerInterface $methodEligibilityChecker)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $methodEligibilityChecker exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
34
    {
35
        $this->methodEligibilityChecker = $methodEligibilityChecker;
36
    }
37
38
    /**
39
     * @param OrderInterface $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     *
41
     * {@inheritdoc}
42
     */
43
    public function validate($order, Constraint $constraint)
44
    {
45
        Assert::isInstanceOf($order, OrderInterface::class);
46
47
        $shipments = $order->getShipments();
48
        if ($shipments->isEmpty()) {
49
            return;
50
        }
51
52
        foreach ($shipments as $shipment) {
53
            if (!$this->methodEligibilityChecker->isEligible($shipment, $shipment->getMethod())) {
54
                $this->context->addViolation(
55
                    $constraint->message,
56
                    ['%shippingMethodName%' => $shipment->getMethod()->getName()]
57
                );
58
            }
59
        }
60
    }
61
}
62