SimpleProductValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Validator;
6
7
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11 View Code Duplication
final class SimpleProductValidator extends ConstraintValidator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * @var ProductRepositoryInterface
15
     */
16
    private $productRepository;
17
18
    /**
19
     * @param ProductRepositoryInterface $productRepository
20
     */
21
    public function __construct(ProductRepositoryInterface $productRepository)
22
    {
23
        $this->productRepository = $productRepository;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function validate($productCode, Constraint $constraint)
30
    {
31
        if (null === $productCode) {
32
            return;
33
        }
34
35
        $product = $this->productRepository->findOneByCode($productCode);
36
37
        if (null === $product || $product->isSimple()) {
38
            return;
39
        }
40
41
        $this->context->addViolation($constraint->message);
42
    }
43
}
44