Skip to content

Commit

Permalink
Refactor controller for SF5. (#9)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Tondereau <[email protected]>
  • Loading branch information
ptondereau and Pierre Tondereau authored Apr 19, 2020
1 parent d8b4531 commit af3c75c
Showing 1 changed file with 18 additions and 61 deletions.
79 changes: 18 additions & 61 deletions src/Controller/RecordsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,45 @@

namespace Fmaj\LaposteDatanovaBundle\Controller;

use Fmaj\LaposteDatanovaBundle\Manager\RecordsManager;
use Fmaj\LaposteDatanovaBundle\Model\Download;
use Fmaj\LaposteDatanovaBundle\Model\Search;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class RecordsController extends Controller
class RecordsController extends AbstractController
{
/**
* @param string $dataset
* @param string $query
* @param string $sort
* @param int $rows
* @param int $start
*
* @return Response
* @var RecordsManager
*/
public function searchAction($dataset, $query, $sort, $rows, $start)
private $recordsManager;

public function __construct(RecordsManager $recordsManager)
{
$this->recordsManager = $recordsManager;
}

public function searchAction(string $dataset, string $query, string $sort, int $rows, int $start): Response
{
$search = new Search($dataset);
$search
->setFilter($query)
->setStart($start)
->setSort($sort)
->setRows($rows);
$results = $this->search($search);
$results = $this->recordsManager->search($search);

return new Response(json_encode($results));
return new JsonResponse($results);
}

/**
* @param string $dataset
* @param string $_format
* @param string $query
*
* @return Response
*/
public function downloadAction($dataset, $_format, $query)
public function downloadAction(string $dataset, string $_format, string $query): Response
{
$response = new Response();
$download = new Download($dataset, $_format);
$download->setFilter($query);
$local = $this->getLocalDataset($download);
if (null !== $local) {
$results = $local;
} else {
$results = $this->download($download);
}
$local = $this->recordsManager->getLocalDatasetContent($download);
$results = $local ?? $this->recordsManager->download($download);
switch (strtolower($_format)) {
case 'json':
$results = json_encode($results);
Expand All @@ -64,40 +57,4 @@ public function downloadAction($dataset, $_format, $query)

return $response;
}

/**
* @param Search $search
*
* @return array
*/
private function search(Search $search)
{
return $this
->get('data_nova.manager.records')
->search($search);
}

/**
* @param Download $download
*
* @return array
*/
private function download(Download $download)
{
return $this
->get('data_nova.manager.records')
->download($download);
}

/**
* @param Download $download
*
* @return null|string
*/
private function getLocalDataset(Download $download)
{
return $this
->get('data_nova.manager.records')
->getLocalDatasetContent($download);
}
}

0 comments on commit af3c75c

Please sign in to comment.