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

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.     public function __construct(ValidatorInterface $validator)
  16.     {
  17.         $this->validator $validator;
  18.     }
  19.     /**
  20.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             PostWriteValidationEvent::class => 'preValidate',
  26.         ];
  27.     }
  28.     public function preValidate(PostWriteValidationEvent $event): void
  29.     {
  30.         $writeException $event->getExceptions();
  31.         $commands $event->getCommands();
  32.         $violationList = new ConstraintViolationList();
  33.         foreach ($commands as $command) {
  34.             if (!($command instanceof InsertCommand) || $command->getDefinition()->getClass() !== LandingPageDefinition::class) {
  35.                 continue;
  36.             }
  37.             if (!$this->hasAnotherValidCommand($commands$command)) {
  38.                 $violationList->addAll(
  39.                     $this->validator->startContext()
  40.                         ->atPath($command->getPath() . '/salesChannels')
  41.                         ->validate(null, [new NotBlank()])
  42.                         ->getViolations()
  43.                 );
  44.                 $writeException->add(new WriteConstraintViolationException($violationList));
  45.             }
  46.         }
  47.     }
  48.     /**
  49.      * @param WriteCommand[] $commands
  50.      */
  51.     private function hasAnotherValidCommand(array $commandsWriteCommand $command): bool
  52.     {
  53.         $isValid false;
  54.         foreach ($commands as $searchCommand) {
  55.             if ($searchCommand->getDefinition()->getClass() === LandingPageSalesChannelDefinition::class && $searchCommand instanceof InsertCommand) {
  56.                 $searchPrimaryKey $searchCommand->getPrimaryKey();
  57.                 $searchLandingPageId $searchPrimaryKey['landing_page_id'] ?? null;
  58.                 $currentPrimaryKey $command->getPrimaryKey();
  59.                 $currentLandingPageId $currentPrimaryKey['id'] ?? null;
  60.                 if ($searchLandingPageId === $currentLandingPageId) {
  61.                     $isValid true;
  62.                 }
  63.             }
  64.         }
  65.         return $isValid;
  66.     }
  67. }