bundles/Suzuki/MediaBundle/Provider/CustomImageProvider.php line 11

Open in your IDE?
  1. <?php
  2. namespace Suzuki\MediaBundle\Provider;
  3. use Sonata\CoreBundle\Validator\ErrorElement;
  4. use Sonata\MediaBundle\Model\MediaInterface;
  5. use Sonata\MediaBundle\Provider\ImageProvider;
  6. use Symfony\Component\HttpFoundation\File\File;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. class CustomImageProvider extends ImageProvider
  9. {
  10.     private const MAX_SIZE 10240000;
  11.     /**
  12.      * @param ErrorElement   $errorElement
  13.      * @param MediaInterface $media
  14.      */
  15.     public function validate(ErrorElement $errorElementMediaInterface $media)
  16.     {
  17.         parent::validate($errorElement$media);
  18.         $size 0;
  19.         if ($media->getBinaryContent() instanceof UploadedFile) {
  20.             $size $media->getBinaryContent()->getClientSize();
  21.         } elseif ($media->getBinaryContent() instanceof File) {
  22.             $size $media->getBinaryContent()->getSize();
  23.         }
  24.         if ($size self::MAX_SIZE) {
  25.             $errorElement
  26.                 ->with('binaryContent')
  27.                 ->addViolation('The file is too big, max size: ' . (self::MAX_SIZE 1024000) . 'MB')
  28.                 ->end();
  29.         }
  30.     }
  31. }