custom/plugins/SwagCmsExtensions/src/Form/Aggregate/FormGroupField/Validation/TypeValidator.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CmsExtensions\Form\Aggregate\FormGroupField\Validation;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  12. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  13. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldDefinition;
  14. use Swag\CmsExtensions\Form\Aggregate\FormGroupField\FormGroupFieldTypeRegistry;
  15. use Swag\CmsExtensions\Util\Administration\FormValidationController;
  16. use Swag\CmsExtensions\Util\Exception\FormValidationPassedException;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. class TypeValidator implements EventSubscriberInterface
  21. {
  22.     private FormGroupFieldTypeRegistry $typeRegistry;
  23.     public function __construct(FormGroupFieldTypeRegistry $typeRegistry)
  24.     {
  25.         $this->typeRegistry $typeRegistry;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             PreWriteValidationEvent::class => 'preValidate',
  31.         ];
  32.     }
  33.     public function preValidate(PreWriteValidationEvent $event): void
  34.     {
  35.         $violationList = new ConstraintViolationList();
  36.         foreach ($event->getCommands() as $command) {
  37.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)) {
  38.                 continue;
  39.             }
  40.             if ($command->getDefinition()->getClass() !== FormGroupFieldDefinition::class) {
  41.                 continue;
  42.             }
  43.             $violationList->addAll($this->validateType($command));
  44.         }
  45.         if ($violationList->count() > 0) {
  46.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  47.             return;
  48.         }
  49.         if ($event->getContext()->hasExtension(FormValidationController::IS_FORM_VALIDATION)) {
  50.             $event->getExceptions()->add(new FormValidationPassedException());
  51.         }
  52.     }
  53.     private function validateType(WriteCommand $command): ConstraintViolationList
  54.     {
  55.         $violationList = new ConstraintViolationList();
  56.         $payload $command->getPayload();
  57.         if (!isset($payload['type'])) {
  58.             return $violationList;
  59.         }
  60.         $type $this->typeRegistry->getType($payload['type'] ?? '');
  61.         if ($type !== null) {
  62.             return $violationList;
  63.         }
  64.         $messageTemplate 'This "type" value (%value%) is invalid.';
  65.         $parameters = ['%value%' => $payload['type'] ?? 'NULL'];
  66.         $violationList->add(new ConstraintViolation(
  67.             \str_replace(\array_keys($parameters), $parameters$messageTemplate),
  68.             $messageTemplate,
  69.             $parameters,
  70.             null,
  71.             \sprintf('%s/type'$command->getPath()),
  72.             null
  73.         ));
  74.         return $violationList;
  75.     }
  76. }