src/EventSubscriber/AudioMetadataSubscriber.php line 29

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\CRM\AudioAsset;
  4. use App\Service\AudioProcessor;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Vich\UploaderBundle\Event\Events;
  7. use Vich\UploaderBundle\Event\Event;
  8. class AudioMetadataSubscriber implements EventSubscriberInterface
  9. {
  10.     private AudioProcessor $audioProcessor;
  11.     private string $projectDir;
  12.     public function __construct(AudioProcessor $audioProcessorstring $projectDir)
  13.     {
  14.         $this->audioProcessor $audioProcessor;
  15.         $this->projectDir $projectDir;
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             Events::POST_UPLOAD => 'onPostUpload',
  21.         ];
  22.     }
  23.     public function onPostUpload(Event $event): void
  24.     {
  25.         $object $event->getObject();
  26.         if (!$object instanceof AudioAsset) {
  27.             return;
  28.         }
  29.         $filePath $this->projectDir '/public/uploads/audio/' $object->getFileName();
  30.         if (file_exists($filePath)) {
  31.             $metadata $this->audioProcessor->extractMetadata($filePath);
  32.             if (isset($metadata['duration'])) {
  33.                 $object->setDuration($metadata['duration']);
  34.             }
  35.         }
  36.     }
  37. }