src/Subscribers/FormUploadFileSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Subscribers;
  3. use App\Entity\User;
  4. use Symfony\Component\Form\FormEvent;
  5. use Symfony\Component\Form\FormEvents;
  6. use App\Services\UploadDocumentService;
  7. use Symfony\Component\HttpFoundation\File\UploadedFile;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * Normalisation des fichiers pour les propriété de l'entité User
  11.  * #idCard
  12.  * #certificate
  13.  */
  14. class FormUploadFileSubscriber implements EventSubscriberInterface {
  15.     private $uploadDocumentService;
  16.     public function __construct(UploadDocumentService $uploadDocumentService) {
  17.         $this->uploadDocumentService $uploadDocumentService;
  18.     }
  19.     public static function getSubscribedEvents() {
  20.         return [
  21.             FormEvents::POST_SUBMIT => 'onSubmit',
  22.         ];
  23.     }
  24.     public function onSubmit(FormEvent $event) {
  25.         /** @var User $user */
  26.         $user $event->getData();
  27.         /** @var  UploadedFile */
  28.         //$idCard = $event->getForm()->get('idCard')->getData();
  29.         /** Sauvegarde de la cart d'identité */
  30.         // if ($event->getForm()->isValid() && $idCard != null) {
  31.         //     $fileName = explode('.',$idCard->getClientOriginalName())[0];
  32.         //     $file = $this->uploadDocumentService->upload($idCard, $fileName);
  33.         //     $user->setIdCard($file);
  34.         // }
  35.         /** Sauvegarde des documents pour une collectivité */
  36.         if ($event->getForm()->isValid() && ($user->getProfile() == 'Collectivité(Préfecture,Mairie)' || $user->getProfile() == "ONG,Association")) {
  37.             $certificate $event->getForm()->get('certificateFiles')->getData();
  38.             if ($certificate != null) {
  39.                 $files $user->getCertificate();
  40.                 foreach ($certificate as $value) {
  41.                     $file $this->uploadDocumentService->upload($value$value->getClientOriginalName());
  42.                    
  43.                     $files[] = $file;
  44.                 }
  45.                 $user->setCertificate($files);
  46.             }
  47.         }
  48.     }
  49. }