vendor/shopware/core/Content/LandingPage/LandingPageValidator.php line 37

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Content\LandingPage;
  3. use Shopware\Core\Content\LandingPage\Aggregate\LandingPageSalesChannel\LandingPageSalesChannelDefinition;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PostWriteValidationEvent;
  7. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\Constraints\NotBlank;
  10. use Symfony\Component\Validator\ConstraintViolationList;
  11. use Symfony\Component\Validator\Validator\ValidatorInterface;
  12. class LandingPageValidator implements EventSubscriberInterface
  13. {
  14.     private ValidatorInterface $validator;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct(ValidatorInterface $validator)
  19.     {
  20.         $this->validator $validator;
  21.     }
  22.     /**
  23.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             PostWriteValidationEvent::class => 'preValidate',
  29.         ];
  30.     }
  31.     public function preValidate(PostWriteValidationEvent $event): void
  32.     {
  33.         $writeException $event->getExceptions();
  34.         $commands $event->getCommands();
  35.         $violationList = new ConstraintViolationList();
  36.         foreach ($commands as $command) {
  37.             if (!($command instanceof InsertCommand) || $command->getDefinition()->getClass() !== LandingPageDefinition::class) {
  38.                 continue;
  39.             }
  40.             if (!$this->hasAnotherValidCommand($commands$command)) {
  41.                 $violationList->addAll(
  42.                     $this->validator->startContext()
  43.                         ->atPath($command->getPath() . '/salesChannels')
  44.                         ->validate(null, [new NotBlank()])
  45.                         ->getViolations()
  46.                 );
  47.                 $writeException->add(new WriteConstraintViolationException($violationList));
  48.             }
  49.         }
  50.     }
  51.     /**
  52.      * @param WriteCommand[] $commands
  53.      */
  54.     private function hasAnotherValidCommand(array $commandsWriteCommand $command): bool
  55.     {
  56.         $isValid false;
  57.         foreach ($commands as $searchCommand) {
  58.             if ($searchCommand->getDefinition()->getClass() === LandingPageSalesChannelDefinition::class && $searchCommand instanceof InsertCommand) {
  59.                 $searchPrimaryKey $searchCommand->getPrimaryKey();
  60.                 $searchLandingPageId $searchPrimaryKey['landing_page_id'] ?? null;
  61.                 $currentPrimaryKey $command->getPrimaryKey();
  62.                 $currentLandingPageId $currentPrimaryKey['id'] ?? null;
  63.                 if ($searchLandingPageId === $currentLandingPageId) {
  64.                     $isValid true;
  65.                 }
  66.             }
  67.         }
  68.         return $isValid;
  69.     }
  70. }