<?php
namespace App\EventSubscriber;
use App\Event\UploadedFileModified;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelInterface;
class UploadedFileSubscriber implements EventSubscriberInterface
{
public function __construct(
private KernelInterface $kernel,
private ParameterBagInterface $parameterBag
) {}
/**
* @inheritDoc
*/
public static function getSubscribedEvents()
{
return [
UploadedFileModified::NAME => 'uploadedFileModified',
];
}
public function uploadedFileModified(UploadedFileModified $event): void
{
$fpFilePath = $event->fsPathPrefix() . '/' . $event->path();
$this->postToUrl(sprintf('%s/purge', $this->parameterBag->get('app.responsive_media_base_url')), $fpFilePath);
}
protected function postToUrl(string $url, string $data)
{
if($this->kernel->getEnvironment() != 'prod') {
return;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
}