Skip to content

Commit

Permalink
Update entity exists validator, deprecate GettableById and FindableBy…
Browse files Browse the repository at this point in the history
…Id interfaces (#61)

* update entity exists validator

* fix names

* update errors handling

---------

Co-authored-by: Vladyslav Yarysh <[email protected]>
  • Loading branch information
gabplch and Vladyslav Yarysh authored May 1, 2024
1 parent 2bfb06b commit bbf36eb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Service/Repository/FindableByIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* FindableByIdInterface.
*
* @deprecated
*/
interface FindableByIdInterface
{
Expand Down
4 changes: 3 additions & 1 deletion Service/Repository/GettableOneByIdInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace StfalconStudio\ApiBundle\Service\Repository;

/**
* Gettable One By Id Interface.
* GettableOneByIdInterface.
*
* @deprecated
*/
interface GettableOneByIdInterface
{
Expand Down
4 changes: 3 additions & 1 deletion Service/Repository/RepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use StfalconStudio\ApiBundle\Traits\EntityManagerTrait;

/**
* Repository Service.
* RepositoryService.
*
* @deprecated
*/
class RepositoryService
{
Expand Down
14 changes: 9 additions & 5 deletions Validator/Constraints/Entity/EntityExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class EntityExists extends Constraint

public string $message = 'entity_does_not_exist';

/** @var class-string<object> */
public string $class;
public string $property;

/**
* @param string $class
* @param mixed $options
* @param array|null $groups
* @param mixed $payload
* @param class-string<object> $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);

Expand All @@ -52,5 +55,6 @@ public function __construct(string $class, mixed $options = null, array $groups
}

$this->class = $class;
$this->property = $property;
}
}
30 changes: 19 additions & 11 deletions Validator/Constraints/Entity/EntityExistsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,12 +23,7 @@
*/
class EntityExistsValidator extends ConstraintValidator
{
/**
* @param RepositoryService $repositoryService
*/
public function __construct(private readonly RepositoryService $repositoryService)
{
}
use EntityManagerTrait;

/**
* @param mixed $value
Expand All @@ -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()
;
}
Expand Down

0 comments on commit bbf36eb

Please sign in to comment.