Refactor data controller to fully use dependency injection

This commit is contained in:
David Molineus
2016-10-06 08:14:08 +02:00
parent bbd97ae47d
commit ea359e8b79
2 changed files with 42 additions and 18 deletions

View File

@@ -40,39 +40,56 @@ class DataController
'filter' => null,
'values' => null
);
/**
* Filters configuration.
*
* @var array
*/
private $filters;
/**
* @var bool
*/
private $displayErrors;
/**
* Construct.
*
* @param MapProvider $mapProvider The map provider.
* @param array $input The user input as array.
* @param MapProvider $mapProvider The map provider.
* @param array $filters Filters configuration.
* @param bool $displayErrors Display errors.
*/
public function __construct(MapProvider $mapProvider, $input)
public function __construct(MapProvider $mapProvider, array $filters, $displayErrors)
{
$this->mapProvider = $mapProvider;
$this->input = array_merge($this->input, $input);
$this->mapProvider = $mapProvider;
$this->filters = $filters;
$this->displayErrors = $displayErrors;
}
/**
* Execute the controller and create the data response.
*
* @param array $input The user input as array.
*
* @return void
*
* @throws \Exception If anything went wrong.
*/
public function execute()
public function execute(array $input)
{
$input = array_merge($this->input, $input);
try {
if ($this->input['filter']) {
$filter = $this->createFilter();
if ($input['filter']) {
$filter = $this->createFilter($input);
} else {
$filter = null;
}
list($data, $error) = $this->loadData($this->input['type'], $this->input['id'], $filter);
$this->encodeData($this->input['format'], $data);
list($data, $error) = $this->loadData($input['type'], $input['id'], $filter);
$this->encodeData($input['format'], $data);
} catch (\Exception $e) {
if (\Config::get('debugMode') || \Config::get('displayErrors')) {
if ($this->displayErrors) {
throw $e;
}
$error = true;
@@ -135,20 +152,22 @@ class DataController
/**
* Create a filter.
*
* @param array $input The user input as array.
*
* @return Filter
* @throws \RuntimeException If the filter is not defined.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function createFilter()
private function createFilter($input)
{
if (!isset($GLOBALS['LEAFLET_FILTERS'][$this->input['filter']])) {
throw new \RuntimeException(sprintf('Undefined filter "%s".', $this->input['filter']));
if (!isset($this->filters[$input['filter']])) {
throw new \RuntimeException(sprintf('Undefined filter "%s".', $input['filter']));
}
/** @var Filter $filter */
$filter = $GLOBALS['LEAFLET_FILTERS'][$this->input['filter']];
$filter = $this->filters[$input['filter']];
return $filter::fromRequest($this->input['values']);
return $filter::fromRequest($input['values']);
}
}

View File

@@ -232,8 +232,13 @@ class MapProvider
return;
}
$controller = new DataController($this, $data);
$controller->execute();
$controller = new DataController(
$this,
$GLOBALS['LEAFLET_FILTERS'],
\Config::get('debugMode') || \Config::get('displayErrors')
);
$controller->execute($data);
if ($exit) {
exit;