Conditions | 6 |
Paths | 5 |
Total Lines | 24 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 15 |
CRAP Score | 6 |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
24 | 7 | public function validate($value, Constraint $constraint): void |
|
25 | { |
||
26 | 7 | if (!$constraint instanceof EntityExist) { |
|
27 | 2 | throw new \LogicException(\sprintf('You can only pass %s constraint to this validator.', EntityExist::class)); |
|
28 | } |
||
29 | |||
30 | 5 | if (null === $value || '' === $value) { |
|
31 | 1 | return; |
|
32 | } |
||
33 | |||
34 | 4 | if (empty($constraint->entity)) { |
|
35 | 1 | throw new \LogicException(\sprintf('Must set "entity" on "%s" validator', EntityExist::class)); |
|
36 | } |
||
37 | |||
38 | 3 | $data = $this->entityManager->getRepository($constraint->entity)->findOneBy([ |
|
39 | 3 | $constraint->property => $value, |
|
40 | ]); |
||
41 | |||
42 | 3 | if (null === $data) { |
|
43 | 1 | $this->context->buildViolation($constraint->message) |
|
|
|||
44 | 1 | ->setParameter('%entity%', $constraint->entity) |
|
45 | 1 | ->setParameter('%property%', $constraint->property) |
|
46 | 1 | ->setParameter('%value%', (string) $value) |
|
47 | 1 | ->addViolation(); |
|
48 | } |
||
51 |