src/App/Subscriber/ValidationSubscriber.php line 110

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Subscriber;
  3. use App\Validator\Constraints\Url;
  4. use App\Validator\Constraints\Video;
  5. use Oneup\UploaderBundle\Event\ValidationEvent;
  6. use Oneup\UploaderBundle\Uploader\Exception\ValidationException;
  7. use Oneup\UploaderBundle\UploadEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\Constraints\File;
  10. use Symfony\Component\Validator\Constraints\Image;
  11. use Symfony\Component\Validator\ConstraintViolationInterface;
  12. use Symfony\Component\Validator\ConstraintViolationListInterface;
  13. use Symfony\Component\Validator\Validator\ValidatorInterface;
  14. use UploadsBundle\Event\ValidationUrlEvent;
  15. class ValidationSubscriber implements EventSubscriberInterface
  16. {
  17.     private ValidatorInterface $validator;
  18.     /**
  19.      * @var string[]
  20.      */
  21.     private array $allowedUrls;
  22.     /**
  23.      * @param string[] $allowedUrls
  24.      */
  25.     public function __construct(ValidatorInterface $validator, array $allowedUrls)
  26.     {
  27.         $this->validator $validator;
  28.         $this->allowedUrls $allowedUrls;
  29.     }
  30.     public function onValidate(ValidationEvent $event): void
  31.     {
  32.         $constraints = [];
  33.         switch ($event->getRequest()->get('domain')) {
  34.             case 'project_preview':
  35.             case 'project_gallery':
  36.             case 'deal_preview':
  37.             case 'deal_gallery':
  38.             case 'shop_video_preview':
  39.             case 'shop_video_gallery':
  40.             case 'tag_preview':
  41.             case 'performer_preview':
  42.             case 'performer_image':
  43.             case 'feed_gallery':
  44.             case 'message_preview':
  45.             case 'message_gallery':
  46.                 $constraints[] = new Image([
  47.                     'maxSize' => '200M',
  48.                     'mimeTypes' => ['image/jpg''image/jpeg''image/png'],
  49.                     'mimeTypesMessage' => 'image.mimeTypesMessage',
  50.                     'minWidth' => 200,
  51.                     'minHeight' => 200,
  52.                 ]);
  53.                 break;
  54.             case 'deal_video':
  55.             case 'deal_trailer':
  56.             case 'project_video':
  57.             case 'project_trailer':
  58.             case 'project_custom_video':
  59.             case 'shop_video':
  60.             case 'shop_video_trailer':
  61.             case 'message_video':
  62.                 $constraints[] = new File([
  63.                     'mimeTypes' => [
  64.                         'video/*',
  65.                         'audio/mp4',
  66.                         'application/x-troff-msvideo'// avi
  67.                     ],
  68.                     'mimeTypesMessage' => 'video.mimeTypesMessage',
  69.                 ]);
  70.                 $constraints[] = new Video([
  71.                     'minDuration' => '60',
  72.                 ]);
  73.                 break;
  74.             case 'feed_trailer':
  75.                 $constraints[] = new File([
  76.                     'mimeTypes' => [
  77.                         'video/*',
  78.                         'audio/mp4',
  79.                         'application/x-troff-msvideo'// avi
  80.                     ],
  81.                     'mimeTypesMessage' => 'video.mimeTypesMessage',
  82.                 ]);
  83.                 $constraints[] = new Video([
  84.                     'minDuration' => '2',
  85.                     'maxDuration' => '198',
  86.                 ]);
  87.                 break;
  88.             default:
  89.                 throw new ValidationException('Unknown domain in ' __FILE__);
  90.         }
  91.         $violations $this->validator->validate($event->getFile()->getPathname(), $constraints);
  92.         if ($violations->count() > 0) {
  93.             throw new ValidationException($this->createMessage($violations));
  94.         }
  95.     }
  96.     public function onValidateUrl(ValidationUrlEvent $event): void
  97.     {
  98.         $constraints = [];
  99.         switch ($event->getRequest()->get('domain')) {
  100.             case 'deal_preview':
  101.             case 'project_preview':
  102.             case 'shop_video_preview':
  103.                 $hosts array_map(static function ($url) {
  104.                     return strpos($url'http') !== false parse_url($urlPHP_URL_HOST) : $url;
  105.                 }, $this->allowedUrls);
  106.                 $constraints[] = new Url(['hosts' => $hosts]);
  107.                 break;
  108.             default:
  109.                 throw new ValidationException('Unknown domain');
  110.         }
  111.         $violations $this->validator->validate($event->getUrl(), $constraints);
  112.         if ($violations->count() > 0) {
  113.             throw new ValidationException($this->createMessage($violations));
  114.         }
  115.     }
  116.     /**
  117.      * @param ConstraintViolationListInterface<ConstraintViolationInterface> $violations List of violations
  118.      */
  119.     private function createMessage(ConstraintViolationListInterface $violations): string
  120.     {
  121.         $messages = [];
  122.         foreach ($violations as $violation) {
  123.             $messages[] = $violation->getMessage();
  124.         }
  125.         return implode('. '$messages);
  126.     }
  127.     /**
  128.      * @return array<string, array<int|string, array<int|string, int|string>|int|string>|string>
  129.      */
  130.     public static function getSubscribedEvents(): array
  131.     {
  132.         return [
  133.             UploadEvents::VALIDATION => 'onValidate',
  134.             ValidationUrlEvent::class => 'onValidateUrl',
  135.         ];
  136.     }
  137. }