<?php
declare(strict_types=1);
namespace App\Cloud\Image\EventSubscriber;
use App\Cloud\Image\Service\ProxyService;
use Pimcore\Event\AssetEvents;
use Pimcore\Event\Model\AssetEvent;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Класс подписчиков на события assets
*/
class AssetSubscriber implements EventSubscriberInterface
{
/**
* @param ProxyService $imageProxyService
* @param \Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface $params
*/
public function __construct(
protected ProxyService $imageProxyService,
protected ContainerBagInterface $params
) {
}
/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
AssetEvents::POST_ADD => 'onPostAdd',
AssetEvents::POST_UPDATE => 'onPostAdd',
];
}
/**
* @param \Pimcore\Event\Model\AssetEvent $event
* @throws \Exception
*/
public function onPostAdd(AssetEvent $event)
{
try {
$asset = $event->getAsset();
if ($asset->getType() === 'image') {
$resizeList = [
'link_album' => $this->params->get('imgproxy')['width']['album'],
'link_photo' => $this->params->get('imgproxy')['width']['photo'],
'link_cover' => $this->params->get('imgproxy')['width']['cover'],
];
foreach ($resizeList as $propertyName => $width) {
$link = $this->imageProxyService->resolveUrl(
$asset->getFullPath(),
[
'width' => $width
]
);
$asset->setProperty($propertyName, 'text', $link);
}
$asset->setProperty(
'link_original',
'text',
$this->params->get('image.uri_s3') . $asset->getFullPath()
);
$asset->save();
} elseif ($asset->getType() === 'audio') {
$asset->setProperty(
'link_original',
'text',
$this->params->get('image.uri_s3') . $asset->getFullPath()
);
$asset->save();
}
} catch (\DomainException $e) {
$asset->delete();
throw new \DomainException($e->getMessage());
}
}
}