<?php
namespace App\EventSubscriber;
use Exception;
use App\Model\Vehicle;
use App\Service\FileService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Event\PostSubmitEvent;
use Symfony\Component\Form\FormEvents;
class VehicleTypeSubscriber implements EventSubscriberInterface
{
private FileService $fileService;
public function __construct(FileService $fileService)
{
$this->fileService = $fileService;
}
/**
* @throws Exception
*/
public function onPostSubmit(PostSubmitEvent $event): void
{
/** @var Vehicle $client */
$vehicle = $event->getData();
$form = $event->getForm();
if (!$vehicle instanceof Vehicle) {
return;
}
$registrationFiles = $form->get('registrationFiles')->getData();
$this->fileService->uploadMultipleFiles($registrationFiles, $vehicle, 'registrationFiles');
$conversionFiles = $form->get('conversionFiles')->getData();
$this->fileService->uploadMultipleFiles($conversionFiles, $vehicle, 'conversionFiles');
$event->setData($vehicle);
}
public static function getSubscribedEvents(): array
{
return [
FormEvents::POST_SUBMIT => 'onPostSubmit',
];
}
}