<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use App\Entity\Image;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Vich\UploaderBundle\Storage\StorageInterface;
class ResolveMediaObjectContentUrlSubscriber implements EventSubscriberInterface
{
private $storage;
public function __construct(StorageInterface $storage)
{
$this->storage = $storage;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE],
];
}
public function onPreSerialize(ViewEvent $event): void
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
$mediaObjects = $controllerResult;
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
return;
}
if($request->getMethod() == Request::METHOD_DELETE){
return;
}
if (($attributes = RequestAttributesExtractor::extractAttributes($request)) && \is_a($attributes['resource_class'], Image::class, true)) {
foreach ($mediaObjects as $mediaObject) {
if (!$mediaObject instanceof Image) {
continue;
}
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
$mediaObject->contentUrl = $schema.$url.$this->storage->resolveUri($mediaObject, 'file');
}
}else{
if (!is_iterable($mediaObjects)) {
$mediaObjects = [$mediaObjects];
}
foreach ($mediaObjects as $mediaObject) {
if (method_exists($mediaObject,"getImage")) {
if(!is_null($mediaObject->getImage())){
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
//var_dump($mediaObject->getImage()->filePath);
$urlContruct = $schema.$url.$this->storage->resolveUri($mediaObject->getImage(), 'file');
$mediaObject->getImage()->contentUrl = $urlContruct;
}
} else if (method_exists($mediaObject,"getImages")){
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
foreach ($mediaObject->getImages() as $element){
$element->contentUrl = $schema.$url.$this->storage->resolveUri($element, 'file');
}
}
if (method_exists($mediaObject,"getRepas")){
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
//var_dump($mediaObject->getImage()->filePath);
foreach ($mediaObject->getRepas() as $repas){
$repas->getImage()->contentUrl = $schema.$url.$this->storage->resolveUri($repas->getImage(), 'file');
}
}
if (method_exists($mediaObject,"getRepas")){
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
//var_dump($mediaObject->getImage()->filePath);
foreach ($mediaObject->getRepas() as $repas){
$repas->getImage()->contentUrl = $schema.$url.$this->storage->resolveUri($repas->getImage(), 'file');
}
}
if (method_exists($mediaObject,"getBackground")){
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
$urlContruct = $schema.$url.$this->storage->resolveUri($mediaObject->getBackground(), 'file');
$mediaObject->getBackground()->contentUrl = $urlContruct;
}
if (method_exists($mediaObject,"getMenu")){
if($mediaObject->getMenu() != null) {
$url = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$schema = "https://";
else
$schema = "http://";
$urlContruct = $schema.$url.$this->storage->resolveUri($mediaObject->getMenu(), 'file');
$mediaObject->getMenu()->contentUrl = $urlContruct;
}
}
}
}
}
}