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..d4a33d0 100644 --- a/Service/Repository/GettableOneByIdInterface.php +++ b/Service/Repository/GettableOneByIdInterface.php @@ -13,7 +13,9 @@ namespace StfalconStudio\ApiBundle\Service\Repository; /** - * Gettable One By Id Interface. + * GettableOneByIdInterface. + * + * @deprecated */ interface GettableOneByIdInterface { diff --git a/Service/Repository/RepositoryService.php b/Service/Repository/RepositoryService.php index 65cf1f0..3d91a63 100644 --- a/Service/Repository/RepositoryService.php +++ b/Service/Repository/RepositoryService.php @@ -16,7 +16,9 @@ use StfalconStudio\ApiBundle\Traits\EntityManagerTrait; /** - * Repository Service. + * RepositoryService. + * + * @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..7aa2213 100644 --- a/Validator/Constraints/Entity/EntityExistsValidator.php +++ b/Validator/Constraints/Entity/EntityExistsValidator.php @@ -12,8 +12,9 @@ namespace StfalconStudio\ApiBundle\Validator\Constraints\Entity; +use StfalconStudio\ApiBundle\Exception\LogicException; 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 +23,7 @@ */ class EntityExistsValidator extends ConstraintValidator { - /** - * @param RepositoryService $repositoryService - */ - public function __construct(private readonly RepositoryService $repositoryService) - { - } + use EntityManagerTrait; /** * @param mixed $value @@ -41,15 +37,27 @@ public function validate(mixed $value, Constraint|EntityExists $constraint): voi throw new UnexpectedConstraintException($constraint, EntityExists::class); } - if (!\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)); } - 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) { + 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) - ->setParameter('%id%', $value) + ->setParameter('%property%', $constraint->property) + ->setParameter('%value%', (string) $value) + ->setParameter('%class%', $constraint->class) ->addViolation() ; }