Completed
Push — master ( 64470f...3a69fd )
by Florian
02:24
created

RecordsController::searchAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 5
crap 2
1
<?php
2
3
namespace Laposte\DatanovaBundle\Controller;
4
5
use Laposte\DatanovaBundle\Model\Download;
6
use Laposte\DatanovaBundle\Model\Search;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class RecordsController extends Controller
11
{
12
    /**
13
     * @param string $dataset
14
     * @param string $query
15
     * @param string $sort
16
     * @param int $rows
17
     * @param int $start
18
     *
19
     * @return Response
20
     */
21
    public function searchAction($dataset, $query, $sort, $rows, $start)
22
    {
23
        $search = new Search($dataset);
24
        $search
25
            ->setFilter($query)
26
            ->setStart($start)
27
            ->setSort($sort)
28
            ->setRows($rows);
29
        $results = $this->search($search);
30
31
        return new Response(json_encode($results));
32
    }
33
34
    /**
35
     * @param string $dataset
36
     * @param string $_format
37
     * @param string $query
38
     *
39
     * @return Response
40
     */
41
    public function downloadAction($dataset, $_format, $query)
42
    {
43
        $response = new Response();
44
        $download = new Download($dataset, $_format);
45
        $download->setFilter($query);
46
        $local = $this->getLocalDataset($download);
47
        if ($local) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $local of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
            $results = $this->getLocalDataset($download);
49
        } else {
50
            $results = $this->download($download);
51
        }
52
        switch (strtolower($_format)) {
53
            case 'json':
54
                $results = json_encode($results);
55
                break;
56
            case 'csv':
57
                $response->headers->set('Content-Type', 'text/csv');
58
                break;
59
        }
60
        $response->setContent($results);
61
        $response->headers->set(
62
            'Content-Disposition',
63
            sprintf('attachment; filename="%s.%s"', $dataset, $_format)
64
        );
65
66
        return $response;
67
    }
68
69
    /**
70
     * @param Search $search
71
     *
72
     * @return array
73
     */
74
    private function search(Search $search)
75
    {
76
        return $this
77
            ->get('data_nova.manager.records')
78
            ->search($search);
79
    }
80
81
    /**
82
     * @param Download $download
83
     *
84
     * @return array
85
     */
86
    private function download(Download $download)
87
    {
88
        return $this
89
            ->get('data_nova.manager.records')
90
            ->download($download);
91
    }
92
93
    /**
94
     * @param Download $download
95
     *
96
     * @return array
97
     */
98
    private function getLocalDataset(Download $download)
99
    {
100
        return $this
101
            ->get('data_nova.manager.records')
102
            ->getLocalDatasetContent($download);
103
    }
104
}
105