<?php
namespace App\EventListener;
use App\Event\ImportCsvEvent;
use App\Import\CSVReader;
use App\Import\ImportGroupMembers;
use App\Service\FileUploader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class ImportGroupMembersListener.
*/
class ImportGroupMembersListener implements EventSubscriberInterface
{
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var FileUploader
*/
private $uploader;
/**
* @var ImportGroupMembers
*/
private $groupMembers;
public function __construct(RequestStack $requestStack, FileUploader $uploader, ImportGroupMembers $groupMembers)
{
$this->requestStack = $requestStack;
$this->uploader = $uploader;
$this->groupMembers = $groupMembers;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
ImportCsvEvent::PRE_UPLOAD => [['uploadFile']]
];
}
/**
* @param ImportCsvEvent $event
*/
public function uploadFile(ImportCsvEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if ('admin_group_partners_import' !== $request->get('_route')) {
return;
}
$groupId = $request->get('group_id');
$file = $event->getFile();
$newFilename = ImportGroupMembers::FILENAME_PREFIX.date('YmdHis').'_GROUPID_'.$groupId.'.'.$file->guessClientExtension();
$this->uploader->upload($file, CSVReader::FLUX_CSV_PATH, $newFilename, true);
$event->uploaded(true);
}
}