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

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