Passed
Push — master ( a8e670...1bf9d1 )
by jelmer
36:40 queued 29:20
created

Export::buildQuery()   C

Complexity

Conditions 12
Paths 128

Size

Total Lines 73
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 34
nc 128
nop 0
dl 0
loc 73
ccs 0
cts 45
cp 0
crap 156
rs 6.7333
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Backend\Modules\Locale\Actions;
4
5
use Backend\Core\Engine\Base\ActionIndex as BackendBaseActionIndex;
6
use Backend\Core\Engine\Model as BackendModel;
7
use Backend\Core\Language\Language as BL;
8
use Backend\Core\Language\Locale;
9
use Backend\Modules\Locale\Engine\Model as BackendLocaleModel;
10
use Symfony\Component\HttpFoundation\Response;
11
12
/**
13
 * This is the export-action, it will create a XML with locale items.
14
 */
15
class Export extends BackendBaseActionIndex
16
{
17
    /**
18
     * Filter variables.
19
     *
20
     * @var array
21
     */
22
    private $filter;
23
24
    /**
25
     * Locale items.
26
     *
27
     * @var array
28
     */
29
    private $locale;
30
31
    /**
32
     * Builds the query for this datagrid.
33
     *
34
     * @return array An array with two arguments containing the query and its parameters.
35
     */
36
    private function buildQuery(): array
37
    {
38
        $parameters = [];
39
40
        // start of query
41
        $query =
42
            'SELECT l.id, l.language, l.application, l.module, l.type, l.name, l.value
43
             FROM locale AS l
44
             WHERE 1';
45
46
        // add language
47
        if ($this->filter['language'] !== null) {
48
            // create an array for the languages, surrounded by quotes (example: 'en')
49
            $languages = [];
50
            foreach ($this->filter['language'] as $key => $val) {
51
                $languages[$key] = '\'' . Locale::fromString($val) . '\'';
52
            }
53
54
            $query .= ' AND l.language IN (' . implode(',', $languages) . ')';
55
        }
56
57
        // add application
58
        if ($this->filter['application'] !== null) {
59
            $query .= ' AND l.application = ?';
60
            $parameters[] = $this->filter['application'];
61
        }
62
63
        // add module
64
        if ($this->filter['module'] !== null) {
65
            $query .= ' AND l.module = ?';
66
            $parameters[] = $this->filter['module'];
67
        }
68
69
        // add type
70
        if ($this->filter['type'] !== null) {
71
            // create an array for the types, surrounded by quotes (example: 'lbl')
72
            $types = [];
73
            foreach ($this->filter['type'] as $key => $val) {
74
                if (in_array($val, BackendLocaleModel::TYPES)) {
75
                    $types[$key] = '\'' . $val . '\'';
76
                }
77
            }
78
79
            $query .= ' AND l.type IN (' . implode(',', $types) . ')';
80
        }
81
82
        // add name
83
        if ($this->filter['name'] !== null) {
84
            $query .= ' AND l.name LIKE ?';
85
            $parameters[] = '%' . $this->filter['name'] . '%';
86
        }
87
88
        // add value
89
        if ($this->filter['value'] !== null) {
90
            $query .= ' AND l.value LIKE ?';
91
            $parameters[] = '%' . $this->filter['value'] . '%';
92
        }
93
94
        // filter checkboxes
95
        if ($this->filter['ids']) {
96
            // make really sure we are working with integers
97
            foreach ($this->filter['ids'] as &$id) {
98
                $id = (int) $id;
99
            }
100
101
            $query .= ' AND l.id IN (' . implode(',', $this->filter['ids']) . ') ';
102
        }
103
104
        // end of query
105
        $query .= ' ORDER BY l.application, l.module, l.name ASC';
106
107
        // cough up
108
        return [$query, $parameters];
109
    }
110
111
    /**
112
     * Create the XML based on the locale items.
113
     *
114
     * @return Response
115
     */
116
    public function getContent(): Response
117
    {
118
        $charset = BackendModel::getContainer()->getParameter('kernel.charset');
119
120
        // create XML
121
        $xmlOutput = BackendLocaleModel::createXMLForExport($this->locale);
122
123
        return new Response(
124
            $xmlOutput,
125
            Response::HTTP_OK,
126
            [
127
                'Content-Disposition' => 'attachment; filename="locale_' . BackendModel::getUTCDate('d-m-Y') . '.xml"',
128
                'Content-Type' => 'application/octet-stream;charset=' . $charset,
129
                'Content-Length' => '' . strlen($xmlOutput),
130
            ]
131
        );
132
    }
133
134
    public function execute(): void
135
    {
136
        $this->checkToken();
137
        $this->setFilter();
138
        $this->setItems();
139
    }
140
141
    /**
142
     * Sets the filter based on the $_GET array.
143
     */
144
    private function setFilter(): void
145
    {
146
        $this->filter['language'] = $this->getRequest()->query->get('language', []);
147
        if (empty($this->filter['language'])) {
148
            $this->filter['language'] = BL::getWorkingLanguage();
149
        }
150
        $this->filter['application'] = $this->getRequest()->query->get('application');
151
        $this->filter['module'] = $this->getRequest()->query->get('module');
152
        $this->filter['type'] = $this->getRequest()->query->get('type', '');
153
        if ($this->filter['type'] === '') {
154
            $this->filter['type'] = null;
155
        }
156
        $this->filter['name'] = $this->getRequest()->query->get('name');
157
        $this->filter['value'] = $this->getRequest()->query->get('value');
158
159
        $ids = $this->getRequest()->query->get('ids', '');
160
        if ($ids === '') {
161
            $ids = [];
162
        } else {
163
            $ids = explode('|', $ids);
164
        }
165
        $this->filter['ids'] = $ids;
166
167
        foreach ($this->filter['ids'] as $id) {
168
            // someone is messing with the url, clear ids
169
            if (!is_numeric($id)) {
170
                $this->filter['ids'] = [];
171
                break;
172
            }
173
        }
174
    }
175
176
    /**
177
     * Build items array and group all items by application, module, type and name.
178
     */
179
    private function setItems(): void
180
    {
181
        list($query, $parameters) = $this->buildQuery();
182
183
        // get locale from the database
184
        $items = (array) $this->get('database')->getRecords($query, $parameters);
185
186
        // init
187
        $this->locale = [];
188
189
        // group by application, module, type and name
190
        foreach ($items as $item) {
191
            $this->locale[$item['application']][$item['module']][$item['type']][$item['name']][] = $item;
192
        }
193
194
        // no need to keep this around
195
        unset($items);
196
    }
197
}
198