custom/plugins/SwkwebProductSet/src/Core/Content/ProductSet/Validation/ChangeSetValidator.php line 35

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swkweb\ProductSet\Core\Content\ProductSet\Validation;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PostWriteValidationEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  9. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\Validator\ConstraintViolation;
  12. use Symfony\Component\Validator\ConstraintViolationList;
  13. abstract class ChangeSetValidator implements EventSubscriberInterface
  14. {
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             PreWriteValidationEvent::class => 'preValidate',
  19.             PostWriteValidationEvent::class => 'postValidate',
  20.         ];
  21.     }
  22.     public function preValidate(PreWriteValidationEvent $event): void
  23.     {
  24.         foreach ($this->filterCommands($event->getCommands()) as $command) {
  25.             if ($command instanceof ChangeSetAware) {
  26.                 $command->requestChangeSet();
  27.             }
  28.         }
  29.     }
  30.     public function postValidate(PostWriteValidationEvent $event): void
  31.     {
  32.         foreach ($this->filterCommands($event->getCommands()) as $command) {
  33.             $violations = new ConstraintViolationList();
  34.             $this->validate($command$violations);
  35.             if ($violations->count()) {
  36.                 $event->getExceptions()->add(
  37.                     new WriteConstraintViolationException($violations$command->getPath())
  38.                 );
  39.             }
  40.         }
  41.     }
  42.     protected function isChanged(WriteCommand $commandstring $field): bool
  43.     {
  44.         return isset($command->getPayload()[$field]);
  45.     }
  46.     /**
  47.      * @return array<string,mixed>|string|mixed|null
  48.      */
  49.     protected function getValue(WriteCommand $commandstring $field)
  50.     {
  51.         if ($command instanceof ChangeSetAware) {
  52.             $changeSet $command->getChangeSet();
  53.             if ($changeSet) {
  54.                 return $changeSet->getAfter($field) ?? $changeSet->getBefore($field);
  55.             }
  56.         }
  57.         return $command->getPayload()[$field] ?? null;
  58.     }
  59.     protected function getValueInt(WriteCommand $commandstring $field): int
  60.     {
  61.         $value $this->getValue($command$field);
  62.         assert(is_numeric($value));
  63.         return (int) $value;
  64.     }
  65.     /**
  66.      * @param array<string,mixed> $parameters
  67.      * @param mixed $invalidValue
  68.      */
  69.     protected function buildViolation(string $template, array $parametersstring $path$invalidValuestring $code): ConstraintViolation
  70.     {
  71.         return new ConstraintViolation(strtr($template$parameters), $template$parametersnull$path$invalidValuenull$code);
  72.     }
  73.     /**
  74.      * @param WriteCommand[] $commands
  75.      *
  76.      * @return WriteCommand[]
  77.      */
  78.     private function filterCommands(array $commands): array
  79.     {
  80.         return array_filter($commands, function ($command) {
  81.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)) {
  82.                 return false;
  83.             }
  84.             if ($command->getDefinition()->getClass() !== $this->getDefinitionClass()) {
  85.                 return false;
  86.             }
  87.             foreach ($this->getFields() as $field) {
  88.                 if ($this->isChanged($command$field)) {
  89.                     return true;
  90.                 }
  91.             }
  92.             return false;
  93.         });
  94.     }
  95.     abstract protected function getDefinitionClass(): string;
  96.     /**
  97.      * @return string[]
  98.      */
  99.     abstract protected function getFields(): array;
  100.     abstract protected function validate(WriteCommand $commandConstraintViolationList $violations): void;
  101. }