From 018356bf55680c0ae3d96ff16d782756870dbba6 Mon Sep 17 00:00:00 2001 From: Vladyslav Yarysh Date: Wed, 1 May 2024 17:10:48 +0300 Subject: [PATCH] update errors handling --- .../Constraints/Entity/EntityExistsValidator.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Validator/Constraints/Entity/EntityExistsValidator.php b/Validator/Constraints/Entity/EntityExistsValidator.php index 7297364..7aa2213 100644 --- a/Validator/Constraints/Entity/EntityExistsValidator.php +++ b/Validator/Constraints/Entity/EntityExistsValidator.php @@ -12,6 +12,7 @@ namespace StfalconStudio\ApiBundle\Validator\Constraints\Entity; +use StfalconStudio\ApiBundle\Exception\LogicException; use StfalconStudio\ApiBundle\Exception\Validator\UnexpectedConstraintException; use StfalconStudio\ApiBundle\Traits\EntityManagerTrait; use Symfony\Component\Validator\Constraint; @@ -36,13 +37,21 @@ public function validate(mixed $value, Constraint|EntityExists $constraint): voi throw new UnexpectedConstraintException($constraint, EntityExists::class); } - if (!(\is_int($value) || \is_string($value))) { - return; + $entityClass = $constraint->class; + + try { + $this->em->getClassMetadata($entityClass); + } catch (\Exception $exception) { + throw new LogicException(sprintf('Class %s is not an Entity', $entityClass)); } $repository = $this->em->getRepository($constraint->class); if (!$repository->findOneBy([$constraint->property => $value]) instanceof $constraint->class) { + if (!(\is_int($value) || \is_string($value) || $value instanceof \Stringable)) { + throw new LogicException(sprintf('Value expected to be int, string or implement %s to find cause of the problem', \Stringable::class)); + } + $this->context ->buildViolation($constraint->message) ->setCode(EntityExists::ENTITY_DOES_NOT_EXIST)