Completed
Push — unused-definitions ( d9908f )
by Kamil
18:33
created

UniqueSimpleProductCodeValidator::validate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
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\Model\ProductInterface;
15
use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19
20
/**
21
 * @author Łukasz Chruściel <[email protected]>
22
 */
23
final class UniqueSimpleProductCodeValidator extends ConstraintValidator
24
{
25
    /**
26
     * @var ProductVariantRepositoryInterface
27
     */
28
    private $productVariantRepository;
29
30
    /**
31
     * @param ProductVariantRepositoryInterface $productVariantRepository
32
     */
33
    public function __construct(ProductVariantRepositoryInterface $productVariantRepository)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $productVariantRepository 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->productVariantRepository = $productVariantRepository;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function validate($value, Constraint $constraint)
42
    {
43
        if (!$value instanceof ProductInterface) {
44
            throw new UnexpectedTypeException($value, ProductInterface::class);
45
        }
46
47
        if (!$value->isSimple()) {
48
            return;
49
        }
50
51
        $existingProductVariant = $this->productVariantRepository->findOneBy(['code' => $value->getCode()]);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $existingProductVariant 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...
52
53
        if (null !== $existingProductVariant && $existingProductVariant->getProduct()->getId() !== $value->getId()) {
54
            $this->context->buildViolation($constraint->message)
55
                ->atPath('code')
56
                ->addViolation()
57
            ;
58
        }
59
    }
60
}
61