From 934e1e66ecc0b6a8c6be8274016ff4540a2dccf9 Mon Sep 17 00:00:00 2001 From: Vladyslav Yarysh Date: Wed, 1 May 2024 16:11:24 +0300 Subject: [PATCH 1/3] update entity exists validator --- Service/Repository/FindableByIdInterface.php | 2 ++ .../Repository/GettableOneByIdInterface.php | 2 ++ Service/Repository/RepositoryService.php | 2 ++ Validator/Constraints/Entity/EntityExists.php | 14 +++++++++----- .../Entity/EntityExistsValidator.php | 19 +++++++++---------- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/Service/Repository/FindableByIdInterface.php b/Service/Repository/FindableByIdInterface.php index 9a6d2cb..9bb2754 100644 --- a/Service/Repository/FindableByIdInterface.php +++ b/Service/Repository/FindableByIdInterface.php @@ -14,6 +14,8 @@ /** * FindableByIdInterface. + * + * @deprecated */ interface FindableByIdInterface { diff --git a/Service/Repository/GettableOneByIdInterface.php b/Service/Repository/GettableOneByIdInterface.php index 7a504d8..6964a2b 100644 --- a/Service/Repository/GettableOneByIdInterface.php +++ b/Service/Repository/GettableOneByIdInterface.php @@ -14,6 +14,8 @@ /** * Gettable One By Id Interface. + * + * @deprecated */ interface GettableOneByIdInterface { diff --git a/Service/Repository/RepositoryService.php b/Service/Repository/RepositoryService.php index 65cf1f0..24d3f2a 100644 --- a/Service/Repository/RepositoryService.php +++ b/Service/Repository/RepositoryService.php @@ -17,6 +17,8 @@ /** * Repository Service. + * + * @deprecated */ class RepositoryService { diff --git a/Validator/Constraints/Entity/EntityExists.php b/Validator/Constraints/Entity/EntityExists.php index e22fabf..a76b98d 100644 --- a/Validator/Constraints/Entity/EntityExists.php +++ b/Validator/Constraints/Entity/EntityExists.php @@ -31,15 +31,18 @@ class EntityExists extends Constraint public string $message = 'entity_does_not_exist'; + /** @var class-string */ public string $class; + public string $property; /** - * @param string $class - * @param mixed $options - * @param array|null $groups - * @param mixed $payload + * @param class-string $class + * @param mixed $options + * @param array|null $groups + * @param mixed $payload + * @param string $property */ - public function __construct(string $class, mixed $options = null, array $groups = null, mixed $payload = null) + public function __construct(string $class, mixed $options = null, array $groups = null, mixed $payload = null, string $property = 'id') { parent::__construct($options, $groups, $payload); @@ -52,5 +55,6 @@ public function __construct(string $class, mixed $options = null, array $groups } $this->class = $class; + $this->property = $property; } } diff --git a/Validator/Constraints/Entity/EntityExistsValidator.php b/Validator/Constraints/Entity/EntityExistsValidator.php index 698d109..7297364 100644 --- a/Validator/Constraints/Entity/EntityExistsValidator.php +++ b/Validator/Constraints/Entity/EntityExistsValidator.php @@ -13,7 +13,7 @@ namespace StfalconStudio\ApiBundle\Validator\Constraints\Entity; use StfalconStudio\ApiBundle\Exception\Validator\UnexpectedConstraintException; -use StfalconStudio\ApiBundle\Service\Repository\RepositoryService; +use StfalconStudio\ApiBundle\Traits\EntityManagerTrait; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; @@ -22,12 +22,7 @@ */ class EntityExistsValidator extends ConstraintValidator { - /** - * @param RepositoryService $repositoryService - */ - public function __construct(private readonly RepositoryService $repositoryService) - { - } + use EntityManagerTrait; /** * @param mixed $value @@ -41,15 +36,19 @@ public function validate(mixed $value, Constraint|EntityExists $constraint): voi throw new UnexpectedConstraintException($constraint, EntityExists::class); } - if (!\is_string($value)) { + if (!(\is_int($value) || \is_string($value))) { return; } - if (!$this->repositoryService->findEntityById($value, $constraint->class) instanceof $constraint->class) { + $repository = $this->em->getRepository($constraint->class); + + if (!$repository->findOneBy([$constraint->property => $value]) instanceof $constraint->class) { $this->context ->buildViolation($constraint->message) ->setCode(EntityExists::ENTITY_DOES_NOT_EXIST) - ->setParameter('%id%', $value) + ->setParameter('%property%', $constraint->property) + ->setParameter('%value%', (string) $value) + ->setParameter('%class%', $constraint->class) ->addViolation() ; } From 29b490a04b9aac6e4d859c847f30cda334bb8b73 Mon Sep 17 00:00:00 2001 From: Vladyslav Yarysh Date: Wed, 1 May 2024 16:27:03 +0300 Subject: [PATCH 2/3] fix names --- Service/Repository/GettableOneByIdInterface.php | 2 +- Service/Repository/RepositoryService.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Service/Repository/GettableOneByIdInterface.php b/Service/Repository/GettableOneByIdInterface.php index 6964a2b..d4a33d0 100644 --- a/Service/Repository/GettableOneByIdInterface.php +++ b/Service/Repository/GettableOneByIdInterface.php @@ -13,7 +13,7 @@ namespace StfalconStudio\ApiBundle\Service\Repository; /** - * Gettable One By Id Interface. + * GettableOneByIdInterface. * * @deprecated */ diff --git a/Service/Repository/RepositoryService.php b/Service/Repository/RepositoryService.php index 24d3f2a..3d91a63 100644 --- a/Service/Repository/RepositoryService.php +++ b/Service/Repository/RepositoryService.php @@ -16,7 +16,7 @@ use StfalconStudio\ApiBundle\Traits\EntityManagerTrait; /** - * Repository Service. + * RepositoryService. * * @deprecated */ From 018356bf55680c0ae3d96ff16d782756870dbba6 Mon Sep 17 00:00:00 2001 From: Vladyslav Yarysh Date: Wed, 1 May 2024 17:10:48 +0300 Subject: [PATCH 3/3] 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)