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

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