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

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.     /**
  23.      * @var FormGroupFieldTypeRegistry
  24.      */
  25.     private $typeRegistry;
  26.     public function __construct(FormGroupFieldTypeRegistry $typeRegistry)
  27.     {
  28.         $this->typeRegistry $typeRegistry;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             PreWriteValidationEvent::class => 'preValidate',
  34.         ];
  35.     }
  36.     public function preValidate(PreWriteValidationEvent $event): void
  37.     {
  38.         $violationList = new ConstraintViolationList();
  39.         foreach ($event->getCommands() as $command) {
  40.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)) {
  41.                 continue;
  42.             }
  43.             if ($command->getDefinition()->getClass() !== FormGroupFieldDefinition::class) {
  44.                 continue;
  45.             }
  46.             $violationList->addAll($this->validateType($command));
  47.         }
  48.         if ($violationList->count() > 0) {
  49.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  50.             return;
  51.         }
  52.         if ($event->getContext()->hasExtension(FormValidationController::IS_FORM_VALIDATION)) {
  53.             $event->getExceptions()->add(new FormValidationPassedException());
  54.         }
  55.     }
  56.     private function validateType(WriteCommand $command): ConstraintViolationList
  57.     {
  58.         $violationList = new ConstraintViolationList();
  59.         $payload $command->getPayload();
  60.         if (!isset($payload['type'])) {
  61.             return $violationList;
  62.         }
  63.         $type $this->typeRegistry->getType($payload['type'] ?? '');
  64.         if ($type !== null) {
  65.             return $violationList;
  66.         }
  67.         $messageTemplate 'This "type" value (%value%) is invalid.';
  68.         $parameters = ['%value%' => $payload['type'] ?? 'NULL'];
  69.         $violationList->add(new ConstraintViolation(
  70.             \str_replace(\array_keys($parameters), $parameters$messageTemplate),
  71.             $messageTemplate,
  72.             $parameters,
  73.             null,
  74.             \sprintf('%s/type'$command->getPath()),
  75.             null
  76.         ));
  77.         return $violationList;
  78.     }
  79. }