Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update entity exists validator, deprecate GettableById and FindableById interfaces #61

Merged
merged 3 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
gabplch marked this conversation as resolved.
Show resolved Hide resolved

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
Loading