src/Cloud/Image/EventSubscriber/AssetSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Cloud\Image\EventSubscriber;
  4. use App\Cloud\Image\Service\ProxyService;
  5. use Pimcore\Event\AssetEvents;
  6. use Pimcore\Event\Model\AssetEvent;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * Класс подписчиков на события assets
  11.  */
  12. class AssetSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @param ProxyService $imageProxyService
  16.      * @param \Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface $params
  17.      */
  18.     public function __construct(
  19.         protected ProxyService $imageProxyService,
  20.         protected ContainerBagInterface $params
  21.     ) {
  22.     }
  23.     /**
  24.      * @return string[]
  25.      */
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             AssetEvents::POST_ADD => 'onPostAdd',
  30.             AssetEvents::POST_UPDATE => 'onPostAdd',
  31.         ];
  32.     }
  33.     /**
  34.      * @param \Pimcore\Event\Model\AssetEvent $event
  35.      * @throws \Exception
  36.      */
  37.     public function onPostAdd(AssetEvent $event)
  38.     {
  39.         try {
  40.             $asset $event->getAsset();
  41.             if ($asset->getType() === 'image') {
  42.                 $resizeList = [
  43.                     'link_album' => $this->params->get('imgproxy')['width']['album'],
  44.                     'link_photo' => $this->params->get('imgproxy')['width']['photo'],
  45.                     'link_cover' => $this->params->get('imgproxy')['width']['cover'],
  46.                 ];
  47.                 foreach ($resizeList as $propertyName => $width) {
  48.                     $link $this->imageProxyService->resolveUrl(
  49.                         $asset->getFullPath(),
  50.                         [
  51.                             'width' => $width
  52.                         ]
  53.                     );
  54.                     $asset->setProperty($propertyName'text'$link);
  55.                 }
  56.                 $asset->setProperty(
  57.                     'link_original',
  58.                     'text',
  59.                     $this->params->get('image.uri_s3') . $asset->getFullPath()
  60.                 );
  61.                 $asset->save();
  62.             } elseif ($asset->getType() === 'audio') {
  63.                 $asset->setProperty(
  64.                     'link_original',
  65.                     'text',
  66.                     $this->params->get('image.uri_s3') . $asset->getFullPath()
  67.                 );
  68.                 $asset->save();
  69.             }
  70.         } catch (\DomainException $e) {
  71.             $asset->delete();
  72.             throw new \DomainException($e->getMessage());
  73.         }
  74.     }
  75. }