Completed
Push — symfony3 ( 06e2ee...17e915 )
by Kamil
46:11 queued 26:57
created

ProductVariantCombinationValidator::matches()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 11
nc 8
nop 2
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\ProductBundle\Validator;
13
14
use Sylius\Component\Product\Checker\ProductVariantsParityCheckerInterface;
15
use Sylius\Component\Product\Model\ProductVariantInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20
/**
21
 * @author Paweł Jędrzejewski <[email protected]>
22
 */
23
class ProductVariantCombinationValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var ProductVariantsParityCheckerInterface
27
     */
28
    private $variantsParityChecker;
29
30
    /**
31
     * @param ProductVariantsParityCheckerInterface $variantsParityChecker
32
     */
33
    public function __construct(ProductVariantsParityCheckerInterface $variantsParityChecker)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $variantsParityChecker 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->variantsParityChecker = $variantsParityChecker;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function validate($value, Constraint $constraint)
42
    {
43
        if (!$value instanceof ProductVariantInterface) {
44
            throw new UnexpectedTypeException($value, ProductVariantInterface::class);
45
        }
46
47
        $product = $value->getProduct();
48
        if (!$product->hasVariants() || !$product->hasOptions()) {
49
            return;
50
        }
51
52
        if ($this->variantsParityChecker->checkParity($value, $product)) {
53
            $this->context->addViolation($constraint->message);
54
        }
55
    }
56
}
57