diff --git a/composer.json b/composer.json index 998e612..de0f01b 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/DependencyInjection/AnzuSystemsCommonExtension.php b/src/DependencyInjection/AnzuSystemsCommonExtension.php index c7da607..74eae20 100644 --- a/src/DependencyInjection/AnzuSystemsCommonExtension.php +++ b/src/DependencyInjection/AnzuSystemsCommonExtension.php @@ -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; @@ -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'], @@ -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]) diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index e194e02..1b16529 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -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') ; diff --git a/src/Validator/Constraints/UniqueEntityDtoValidator.php b/src/Validator/Constraints/UniqueEntityDtoValidator.php index 793d920..1427021 100644 --- a/src/Validator/Constraints/UniqueEntityDtoValidator.php +++ b/src/Validator/Constraints/UniqueEntityDtoValidator.php @@ -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; @@ -16,6 +17,7 @@ final class UniqueEntityDtoValidator extends ConstraintValidator public function __construct( private readonly PropertyAccessorInterface $propertyAccessor, private readonly EntityManagerInterface $entityManager, + private readonly string $userEntityClass, ) { } @@ -31,13 +33,20 @@ public function validate(mixed $value, Constraint $constraint): void throw new UnexpectedTypeException($constraint->entity, BaseIdentifiableInterface::class); } + /** @var class-string $entityClass */ + $entityClass = $constraint->entity; + if ($entityClass === AnzuUser::class) { + /** @var class-string $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; }