src/EventSubscriber/UploadedFileSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\UploadedFileModified;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelInterface;
  7. class UploadedFileSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(
  10.         private KernelInterface $kernel,
  11.         private ParameterBagInterface $parameterBag
  12.     ) {}
  13.     /**
  14.      * @inheritDoc
  15.      */
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             UploadedFileModified::NAME => 'uploadedFileModified',
  20.         ];
  21.     }
  22.     public function uploadedFileModified(UploadedFileModified $event): void
  23.     {
  24.         $fpFilePath $event->fsPathPrefix() . '/' $event->path();
  25.         $this->postToUrl(sprintf('%s/purge'$this->parameterBag->get('app.responsive_media_base_url')), $fpFilePath);
  26.     }
  27.     protected function postToUrl(string $urlstring $data)
  28.     {
  29.         if($this->kernel->getEnvironment() != 'prod') {
  30.             return;
  31.         }
  32.         $ch curl_init();
  33.         curl_setopt($chCURLOPT_URL,$url);
  34.         curl_setopt($chCURLOPT_POST1);
  35.         curl_setopt($chCURLOPT_POSTFIELDS$data);
  36.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  37.         curl_exec($ch);
  38.         curl_close($ch);
  39.     }
  40. }