src/EventSubscriber/VehicleTypeSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Exception;
  4. use App\Model\Vehicle;
  5. use App\Service\FileService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\Event\PostSubmitEvent;
  8. use Symfony\Component\Form\FormEvents;
  9. class VehicleTypeSubscriber implements EventSubscriberInterface
  10. {
  11.     private FileService $fileService;
  12.     public function __construct(FileService $fileService)
  13.     {
  14.         $this->fileService $fileService;
  15.     }
  16.     /**
  17.      * @throws Exception
  18.      */
  19.     public function onPostSubmit(PostSubmitEvent $event): void
  20.     {
  21.         /** @var Vehicle $client */
  22.         $vehicle $event->getData();
  23.         $form $event->getForm();
  24.         if (!$vehicle instanceof Vehicle) {
  25.             return;
  26.         }
  27.         $registrationFiles $form->get('registrationFiles')->getData();
  28.         $this->fileService->uploadMultipleFiles($registrationFiles$vehicle'registrationFiles');
  29.         $conversionFiles $form->get('conversionFiles')->getData();
  30.         $this->fileService->uploadMultipleFiles($conversionFiles$vehicle'conversionFiles');
  31.         $event->setData($vehicle);
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             FormEvents::POST_SUBMIT => 'onPostSubmit',
  37.         ];
  38.     }
  39. }