add method helper on manager to simplify controller

The controller does not have to handle data of the form ExportType. The ExportManager now handles those data. This is supposed to limit the number of place where the form data are handled.
This commit is contained in:
Julien Fastré 2016-01-12 17:34:15 +01:00
parent ce2119ee6f
commit fa1be795ec
2 changed files with 32 additions and 7 deletions

View File

@ -25,11 +25,8 @@ namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Form\Type\Export\PickFormatterType;
use Chill\MainBundle\Form\Type\Export\FormatterType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
/**
@ -108,6 +105,9 @@ class ExportController extends Controller
*/
protected function createCreateFormExport($alias, $step, $data = array())
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$builder = $this->get('form.factory')
->createNamedBuilder(null, FormType::class, array(), array(
'method' => 'GET',
@ -125,9 +125,11 @@ class ExportController extends Controller
if ($step === 'formatter') {
$builder->add('formatter', FormatterType::class, array(
'formatter_alias' => $data['export']['pick_formatter']['alias'],
'formatter_alias' => $exportManager
->getFormatterAlias($data['export']),
'export_alias' => $alias,
'aggregator_aliases' => array() //TODO
'aggregator_aliases' => $exportManager
->getUsedAggregatorsAliases($data['export'])
));
}
@ -171,7 +173,6 @@ class ExportController extends Controller
if ($exportForm->isValid()) {
$data = $exportForm->getData();
$this->get('session')->set('export_step', $data);
$this->get('session')->set('export_step_raw', $request->query->all());
$form = $this->createCreateFormExport($alias,

View File

@ -257,7 +257,7 @@ class ExportManager
* @param mixed[] $data
* @return Response
*/
public function generate($exportAlias, array $data)
public function generate($exportAlias, array $data, array $formatterData)
{
$export = $this->getExport($exportAlias);
$qb = $this->em->createQueryBuilder();
@ -288,6 +288,30 @@ class ExportManager
$filters, $aggregators, array(), $data['filters'], $aggregatorsData);
}
/**
* get the aggregators typse used in the form export data
*
* @param array $data the data from the export form
* @return string[]
*/
public function getUsedAggregatorsAliases(array $data)
{
$aggregators = $this->retrieveUsedAggregators($data['aggregators']);
return array_keys(iterator_to_array($aggregators));
}
/**
* get the formatter alias from the form export data
*
* @param array $data the data from the export form
* @string the formatter alias
*/
public function getFormatterAlias(array $data)
{
return $data['pick_formatter']['alias'];
}
/**
* parse the data to retrieve the used filters and aggregators
*