Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
"symfony/http-kernel": "^6.0|^7.0",
"symfony/lock": "^6.0|^7.0",
"symfony/messenger": "^6.0|^7.0",
"symfony/monolog-bundle": ">=3.7",
"symfony/monolog-bundle": ">=3.9",
"symfony/polyfill-uuid": "^1.23",
"symfony/runtime": "^6.0|^7.0",
"symfony/security-bundle": "^6.0|^7.0",
"symfony/uid": "^6.0|^7.0",
"symfony/validator": "^6.0|^7.0",
"symfony/var-exporter": "^6.4|^7.0",
"symfony/yaml": "^6.0|^7.0"
},
"require-dev": {
Expand Down
13 changes: 9 additions & 4 deletions src/DependencyInjection/AnzuSystemsCommonExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
use AnzuSystems\CommonBundle\Serializer\Handler\Handlers\ValueObjectHandler;
use AnzuSystems\CommonBundle\Serializer\Service\BsonConverter;
use AnzuSystems\CommonBundle\Util\ResourceLocker;
use AnzuSystems\CommonBundle\Validator\Constraints\UniqueEntityDtoValidator;
use AnzuSystems\CommonBundle\Validator\Validator;
use AnzuSystems\SerializerBundle\Metadata\MetadataRegistry;
use AnzuSystems\SerializerBundle\Serializer;
Expand Down Expand Up @@ -153,20 +154,20 @@ public function prepend(ContainerBuilder $container): void
'id' => 'anzu_systems_common.logs.audit_log_messenger_handler',
],
'journal_sync' => [
'type' => 'mongo',
'type' => 'mongodb',
'channels' => 'journal_sync',
'level' => 'debug',
'mongo' => [
'mongodb' => [
'id' => 'anzu_systems_common.logs.journal_log_client',
'database' => $logs['journal']['mongo']['database'],
'collection' => $logs['journal']['mongo']['collection'],
],
],
'audit_sync' => [
'type' => 'mongo',
'type' => 'mongodb',
'channels' => 'audit_sync',
'level' => 'debug',
'mongo' => [
'mongodb' => [
'id' => 'anzu_systems_common.logs.audit_log_client',
'database' => $logs['audit']['mongo']['database'],
'collection' => $logs['audit']['mongo']['collection'],
Expand Down Expand Up @@ -265,6 +266,10 @@ private function loadSettings(ContainerBuilder $container): void
->getDefinition(CurrentAnzuUserProvider::class)
->replaceArgument('$userEntityClass', $settings['user_entity_class']);

$container
->getDefinition(UniqueEntityDtoValidator::class)
->replaceArgument('$userEntityClass', $settings['user_entity_class']);

if ($settings['send_context_id_with_response']) {
$container->register(ContextIdOnResponseListener::class)
->addTag('kernel.event_listener', ['event' => KernelEvents::RESPONSE])
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
$services->set(UniqueEntityDtoValidator::class)
->arg('$propertyAccessor', service(PropertyAccessorInterface::class))
->arg('$entityManager', service(EntityManagerInterface::class))
->arg('$userEntityClass', null)
->tag('validator.constraint_validator')
;

Expand Down
11 changes: 10 additions & 1 deletion src/Validator/Constraints/UniqueEntityDtoValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AnzuSystems\CommonBundle\Validator\Constraints;

use AnzuSystems\Contracts\Entity\AnzuUser;
use AnzuSystems\Contracts\Entity\Interfaces\BaseIdentifiableInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -16,6 +17,7 @@ final class UniqueEntityDtoValidator extends ConstraintValidator
public function __construct(
private readonly PropertyAccessorInterface $propertyAccessor,
private readonly EntityManagerInterface $entityManager,
private readonly string $userEntityClass,
) {
}

Expand All @@ -31,13 +33,20 @@ public function validate(mixed $value, Constraint $constraint): void
throw new UnexpectedTypeException($constraint->entity, BaseIdentifiableInterface::class);
}

/** @var class-string<BaseIdentifiableInterface> $entityClass */
$entityClass = $constraint->entity;
if ($entityClass === AnzuUser::class) {
/** @var class-string<BaseIdentifiableInterface> $entityClass */
$entityClass = $this->userEntityClass;
}

$fieldsNames = $constraint->fields;
$fields = [];
foreach ($fieldsNames as $fieldName) {
$fields[$fieldName] = $this->propertyAccessor->getValue($value, $fieldName);
}
/** @var BaseIdentifiableInterface|null $existingEntity */
$existingEntity = $this->entityManager->getRepository($constraint->entity)->findOneBy($fields);
$existingEntity = $this->entityManager->getRepository($entityClass)->findOneBy($fields);
if (null === $existingEntity) {
return;
}
Expand Down