custom/plugins/SolidProductVideos/src/Core/Content/Product/Subscriber/ProductEmbeddedVideoMediaValidator.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace StudioSolid\ProductVideos\Core\Content\Product\Subscriber;
  3. use Exception;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  6. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  7. use StudioSolid\ProductVideos\Core\Content\Product\Aggregate\ProductEmbeddedVideoMedia\ProductEmbeddedVideoMediaDefinition;
  8. use StudioSolid\ProductVideos\Core\Content\Product\Service\ProductEmbeddedVideoMediaService;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Validator\ConstraintViolation;
  11. use Symfony\Component\Validator\ConstraintViolationList;
  12. class ProductEmbeddedVideoMediaValidator implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var ProductEmbeddedVideoMediaService
  16.      */
  17.     private $productEmbeddedVideoMediaService;
  18.     public function __construct(ProductEmbeddedVideoMediaService $productEmbeddedVideoMediaService)
  19.     {
  20.         $this->productEmbeddedVideoMediaService $productEmbeddedVideoMediaService;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             PreWriteValidationEvent::class => 'validate',
  26.         ];
  27.     }
  28.     public function validate(PreWriteValidationEvent $event): void
  29.     {
  30.         $violations = new ConstraintViolationList();
  31.         foreach ($event->getCommands() as $command) {
  32.             if (!($command instanceof InsertCommand)) {
  33.                 continue;
  34.             }
  35.             if ($command->getDefinition()->getClass() !== ProductEmbeddedVideoMediaDefinition::class) {
  36.                 continue;
  37.             }
  38.             $payload $command->getPayload();
  39.             $source $payload['source'];
  40.             $videoId $payload['video_id'];
  41.             try {
  42.                 $this->productEmbeddedVideoMediaService->validate($source$videoId);
  43.             } catch (Exception $exception) {
  44.                 $violations->add(new ConstraintViolation(
  45.                     $exception->getMessage(),
  46.                     null,
  47.                     [],
  48.                     null,
  49.                     '/',
  50.                     null
  51.                 ));
  52.             }
  53.             try {
  54.                 if (!$this->productEmbeddedVideoMediaService->fetchThumbnailPlatformUrl($source$videoId)) {
  55.                     throw new \InvalidArgumentException('Parameter videoId is not available at the requested source ' $source);
  56.                 }
  57.             } catch (Exception $exception) {
  58.                 $violations->add(new ConstraintViolation(
  59.                     $exception->getMessage(),
  60.                     null,
  61.                     [],
  62.                     null,
  63.                     '/',
  64.                     null
  65.                 ));
  66.             }
  67.         }
  68.         if ($violations->count() < 1) {
  69.             return;
  70.         }
  71.         $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  72.     }
  73. }