src/EventListener/ImportGroupMembersListener.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Event\ImportCsvEvent;
  4. use App\Import\CSVReader;
  5. use App\Import\ImportGroupMembers;
  6. use App\Service\FileUploader;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. /**
  10.  * Class ImportGroupMembersListener.
  11.  */
  12. class ImportGroupMembersListener implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var RequestStack
  16.      */
  17.     private $requestStack;
  18.     /**
  19.      * @var FileUploader
  20.      */
  21.     private $uploader;
  22.     /**
  23.      * @var ImportGroupMembers
  24.      */
  25.     private $groupMembers;
  26.     public function __construct(RequestStack $requestStackFileUploader $uploaderImportGroupMembers $groupMembers)
  27.     {
  28.         $this->requestStack $requestStack;
  29.         $this->uploader $uploader;
  30.         $this->groupMembers $groupMembers;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             ImportCsvEvent::PRE_UPLOAD => [['uploadFile']]
  39.         ];
  40.     }
  41.     /**
  42.      * @param ImportCsvEvent $event
  43.      */
  44.     public function uploadFile(ImportCsvEvent $event)
  45.     {
  46.         $request $this->requestStack->getCurrentRequest();
  47.         if ('admin_group_partners_import' !== $request->get('_route')) {
  48.             return;
  49.         }
  50.         $groupId $request->get('group_id');
  51.         $file $event->getFile();
  52.         $newFilename ImportGroupMembers::FILENAME_PREFIX.date('YmdHis').'_GROUPID_'.$groupId.'.'.$file->guessClientExtension();
  53.         $this->uploader->upload($fileCSVReader::FLUX_CSV_PATH$newFilenametrue);
  54.         $event->uploaded(true);
  55.     }
  56. }