ProductNotInComparerValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\SyliusComparerPlugin\Validator\Constraints;
6
7
use Locastic\SyliusComparerPlugin\Request\AddProductToComparerRequest;
8
use Sylius\Component\Core\Model\ProductInterface;
9
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
10
use Symfony\Component\Validator\Constraint;
11
use Symfony\Component\Validator\ConstraintValidator;
12
13
final class ProductNotInComparerValidator extends ConstraintValidator
14
{
15
    /** @var ProductRepositoryInterface */
16
    private $productRepository;
17
18
    public function __construct(ProductRepositoryInterface $productRepository)
19
    {
20
        $this->productRepository = $productRepository;
21
    }
22
23
    /**
24
     * @param AddProductToComparerRequest $requestData
25
     * @param Constraint $constraint
26
     */
27
    public function validate($requestData, Constraint $constraint)
28
    {
29
        /** @var ProductInterface $product */
30
        $product = $this->productRepository->findOneBy(['id' => $requestData->getProductId()]);
31
        $comparer = $requestData->getComparer();
32
        if ($comparer->hasProduct($product)) {
33
            $this->context->buildViolation($constraint->message)
34
                ->atPath('productExistsMessage')
35
                ->setParameter('{{ product }}', $product->getName())
36
                ->addViolation();
37
        }
38
    }
39
}
40