rewrite export controller and manager

- upgrade to symfony 4 ;
- add support for directExportInterface
This commit is contained in:
Julien Fastré 2019-01-15 20:18:47 +01:00
parent f937e9d12c
commit 6741eb949c
3 changed files with 130 additions and 48 deletions

View File

@ -29,6 +29,10 @@ use Chill\MainBundle\Form\Type\Export\FormatterType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Chill\MainBundle\Export\ExportManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Form\FormFactoryInterface;
/**
* ExportController is the controller use for exporting data.
@ -37,6 +41,44 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
*/
class ExportController extends Controller
{
/**
*
* @var ExportManager
*/
protected $exportManager;
/**
*
* @var LoggerInterface
*/
protected $logger;
/**
*
* @var SessionInterface
*/
protected $session;
/**
*
* @var FormFactoryInterface
*/
protected $formFactory;
public function __construct(
ExportManager $exportManager,
FormFactoryInterface $formFactory,
LoggerInterface $logger,
SessionInterface $session
) {
$this->exportManager = $exportManager;
$this->formFactory = $formFactory;
$this->logger = $logger;
$this->session = $session;
}
/**
* Render the list of available exports
*
@ -45,7 +87,7 @@ class ExportController extends Controller
*/
public function indexAction(Request $request)
{
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$exports = $exportManager->getExports(true);
@ -73,7 +115,7 @@ class ExportController extends Controller
public function newAction(Request $request, $alias)
{
// first check for ACL
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$export = $exportManager->getExport($alias);
if ($exportManager->isGrantedForElement($export) === FALSE) {
@ -84,34 +126,40 @@ class ExportController extends Controller
switch ($step) {
case 'centers':
return $this->selectCentersStep($request, $alias);
return $this->selectCentersStep($request, $export, $alias);
case 'export':
return $this->exportFormStep($request, $alias);
return $this->exportFormStep($request, $export, $alias);
break;
case 'formatter':
return $this->formatterFormStep($request, $alias);
return $this->formatterFormStep($request, $export, $alias);
break;
case 'generate':
return $this->forwardToGenerate($request, $alias);
return $this->forwardToGenerate($request, $export, $alias);
break;
default:
throw $this->createNotFoundException("The given step '$step' is invalid");
}
}
public function selectCentersStep(Request $request, $alias)
/**
*
* @param Request $request
* @param \Chill\MainBundle\Export\ExportInterface|\Chill\MainBundle\Export\DirectExportInterface $export
* @param string $alias
* @return Response
* @throws type
*/
protected function selectCentersStep(Request $request, $export, $alias)
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$form = $this->createCreateFormExport($alias, 'centers');
$export = $exportManager->getExport($alias);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('logger')->debug('form centers is valid', array(
$this->logger->debug('form centers is valid', array(
'location' => __METHOD__));
$data = $form->getData();
@ -123,12 +171,12 @@ class ExportController extends Controller
. 'access to this export for those centers');
}
$this->get('session')->set('centers_step_raw',
$this->session->set('centers_step_raw',
$request->request->all());
$this->get('session')->set('centers_step', $data);
$this->session->set('centers_step', $data);
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('centers'),
'step' => $this->getNextStep('centers', $export),
'alias' => $alias
));
@ -149,19 +197,20 @@ class ExportController extends Controller
* is done to next step.
*
* @param string $alias
* @param \Chill\MainBundle\Export\ExportInterface|\Chill\MainBundle\Export\DirectExportInterface $export
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function exportFormStep(Request $request, $alias)
protected function exportFormStep(Request $request, $export, $alias)
{
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
// check we have data from the previous step (export step)
$data = $this->get('session')->get('centers_step', null);
$data = $this->session->get('centers_step', null);
if ($data === null) {
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('export', true),
'step' => $this->getNextStep('export', $export, true),
'alias' => $alias
));
}
@ -174,23 +223,23 @@ class ExportController extends Controller
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('logger')->debug('form export is valid', array(
$this->logger->debug('form export is valid', array(
'location' => __METHOD__));
// store data for reusing in next steps
$data = $form->getData();
$this->get('session')->set('export_step_raw',
$this->session->set('export_step_raw',
$request->request->all());
$this->get('session')->set('export_step', $data);
$this->session->set('export_step', $data);
//redirect to next step
return $this->redirect(
$this->generateUrl('chill_main_export_new', array(
'step' => $this->getNextStep('export'),
'step' => $this->getNextStep('export', $export),
'alias' => $alias
)));
} else {
$this->get('logger')->debug('form export is invalid', array(
$this->logger->debug('form export is invalid', array(
'location' => __METHOD__));
}
}
@ -213,10 +262,10 @@ class ExportController extends Controller
protected function createCreateFormExport($alias, $step, $data = array())
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$isGenerate = strpos($step, 'generate_') === 0;
$builder = $this->get('form.factory')
$builder = $this->formFactory
->createNamedBuilder(null, FormType::class, array(), array(
'method' => $isGenerate ? 'GET' : 'POST',
'csrf_protection' => $isGenerate ? false : true,
@ -258,11 +307,12 @@ class ExportController extends Controller
* This method provides a centralized way of handling next/previous step.
*
* @param string $step the current step
* @param \Chill\MainBundle\Export\ExportInterface|\Chill\MainBundle\Export\DirectExportInterface $export
* @param boolean $reverse set to true to get the previous step
* @return string the next/current step
* @throws \LogicException if there is no step before or after the given step
*/
private function getNextStep($step, $reverse = false)
private function getNextStep($step, $export, $reverse = false)
{
switch($step) {
case 'centers':
@ -271,7 +321,12 @@ class ExportController extends Controller
}
return 'export';
case 'export':
return $reverse ? 'centers' : 'formatter';
if ($export instanceof \Chill\MainBundle\Export\ExportInterface) {
return $reverse ? 'centers' : 'formatter';
} elseif ($export instanceof \Chill\MainBundle\Export\DirectExportInterface) {
return $reverse ? 'centers' : 'generate';
}
case 'formatter' :
return $reverse ? 'export' : 'generate';
case 'generate' :
@ -292,20 +347,20 @@ class ExportController extends Controller
* redirect to the next step.
*
* @param Request $request
* @param \Chill\MainBundle\Export\ExportInterface|\Chill\MainBundle\Export\DirectExportInterface $export
* @param string $alias
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function formatterFormStep(Request $request, $alias)
protected function formatterFormStep(Request $request, $export, $alias)
{
$export = $this->get('chill.main.export_manager')->getExport($alias);
// check we have data from the previous step (export step)
$data = $this->get('session')->get('export_step', null);
$data = $this->session->get('export_step', null);
if ($data === null) {
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('formatter', true),
'step' => $this->getNextStep('formatter', $export, true),
'alias' => $alias
));
}
@ -317,15 +372,15 @@ class ExportController extends Controller
if ($form->isValid()) {
$dataFormatter = $form->getData();
$this->get('session')->set('formatter_step', $dataFormatter);
$this->get('session')->set('formatter_step_raw',
$this->session->set('formatter_step', $dataFormatter);
$this->session->set('formatter_step_raw',
$request->request->all());
//redirect to next step
return $this->redirect($this->generateUrl('chill_main_export_new',
array(
'alias' => $alias,
'step' => $this->getNextStep('formatter')
'step' => $this->getNextStep('formatter', $export)
)));
}
}
@ -345,26 +400,27 @@ class ExportController extends Controller
* The data from previous steps is removed from session.
*
* @param Request $request
* @param \Chill\MainBundle\Export\ExportInterface|\Chill\MainBundle\Export\DirectExportInterface $export
* @param string $alias
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function forwardToGenerate(Request $request, $alias)
protected function forwardToGenerate(Request $request, $export, $alias)
{
$dataCenters = $this->get('session')->get('centers_step_raw', null);
$dataFormatter = $this->get('session')->get('formatter_step_raw', null);
$dataExport = $this->get('session')->get('export_step_raw', null);
$dataCenters = $this->session->get('centers_step_raw', null);
$dataFormatter = $this->session->get('formatter_step_raw', null);
$dataExport = $this->session->get('export_step_raw', null);
if ($dataFormatter === NULL) {
return $this->redirectToRoute('chill_main_export_new', array(
'alias' => $alias, 'step' => $this->getNextStep('generate', true)
'alias' => $alias, 'step' => $this->getNextStep('generate', $export, true)
));
}
// remove data from session
$this->get('session')->remove('export_step_raw');
$this->get('session')->remove('export_step');
$this->get('session')->remove('formatter_step_raw');
$this->get('session')->remove('formatter_step');
$this->session->remove('export_step_raw');
$this->session->remove('export_step');
$this->session->remove('formatter_step_raw');
$this->session->remove('formatter_step');
$redirectParameters = array_merge(
$dataFormatter,
@ -390,7 +446,7 @@ class ExportController extends Controller
public function generateAction(Request $request, $alias)
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
$formCenters->handleRequest($request);
@ -414,7 +470,7 @@ class ExportController extends Controller
public function downloadResultAction(Request $request, $alias)
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$exportManager = $this->exportManager;
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
$formCenters->handleRequest($request);
$dataCenters = $formCenters->getData();

View File

@ -0,0 +1,15 @@
<?php
/*
*
*/
namespace Chill\MainBundle\Export;
use Symfony\Component\HttpFoundation\Response;
/**
*
*/
interface DirectExportInterface extends ExportElementInterface
{
//put your code here
}

View File

@ -148,12 +148,18 @@ class ExportManager
*
* @internal used by DI
*
* @param ExportInterface $export
* @param ExportInterface|DirectExportInterface $export
* @param type $alias
*/
public function addExport(ExportInterface $export, $alias)
public function addExport($export, $alias)
{
$this->exports[$alias] = $export;
if ($export instanceof ExportInterface || $export instanceof DirectExportInterface) {
$this->exports[$alias] = $export;
} else {
throw new \InvalidArgumentException(sprintf("The export with alias %s "
. "does not implements %s or %s.", $alias, ExportInterface::class,
DirectExportInterface::class));
}
}
/**
@ -426,6 +432,11 @@ class ExportManager
//$qb = $this->em->createQueryBuilder();
$centers = $this->getPickedCenters($pickedCentersData);
if ($export instanceof DirectExportInterface) {
return $export->generate($this->buildCenterReachableScopes($centers, $export),
$data[ExportType::EXPORT_KEY]);
}
$query = $export->initiateQuery(
$this->retrieveUsedModifiers($data),
$this->buildCenterReachableScopes($centers, $export),