Conditions | 9 |
Paths | 86 |
Total Lines | 74 |
Lines | 3 |
Ratio | 4.05 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
175 | public function runAction() |
||
176 | { |
||
177 | $sql = $this->Request()->get('query'); |
||
178 | $download = $this->Request()->get('download'); |
||
179 | $rowsetKey = $this->Request()->get('rowset'); |
||
180 | |||
181 | try { |
||
182 | /** @var \mysqli|\Zend_Db_Statement_Pdo $query */ |
||
183 | $query = $this->container->get('wbm_query_manager.db')->query($sql); |
||
184 | $data = array(); |
||
185 | $i = 0; |
||
186 | |||
187 | do { |
||
188 | $data[$i]['rowsetKey'] = $i; |
||
189 | |||
190 | if($this->container->get('wbm_query_manager.db')->getColumnCount($query)){ |
||
191 | $records = $this->container->get('wbm_query_manager.db')->fetchAll($query); |
||
192 | $data[$i]['rowCount'] = $this->container->get('wbm_query_manager.db')->getRowCount($query); |
||
193 | |||
194 | if($download && $rowsetKey != $i){ |
||
195 | $i++; |
||
196 | continue; |
||
197 | } |
||
198 | |||
199 | $recordFields = array_keys($records[0]); |
||
200 | $columns = array(); |
||
201 | foreach($recordFields as $recordField){ |
||
202 | $columns[] = array( |
||
203 | 'header' => $recordField, |
||
204 | 'dataIndex' => $recordField, |
||
205 | 'flex' => 1 |
||
206 | ); |
||
207 | } |
||
208 | |||
209 | $data[$i]['fetchData'] = array( |
||
210 | 'records' => $records, |
||
211 | 'recordFields' => $recordFields, |
||
212 | 'columns' => $columns |
||
213 | ); |
||
214 | |||
215 | if($download){ |
||
216 | $this->container->get('plugins')->Controller()->ViewRenderer()->setNoRender(); |
||
217 | $now = new \DateTime(); |
||
218 | $file = "query_" . $now->format('Y_m_d_h_i_s') . ".csv"; |
||
219 | header("Content-Type: text/csv"); |
||
220 | header("Content-Disposition: attachment; filename=\"$file\""); |
||
221 | |||
222 | $outputBuffer = fopen("php://output", 'w'); |
||
223 | View Code Duplication | foreach(array_merge(array(0 => $recordFields),$records) as $val) { |
|
224 | fputcsv($outputBuffer, $val, $this->container->get('config')->getByNamespace('WbmQueryManager', 'csv_field_separator')); |
||
225 | } |
||
226 | fclose($outputBuffer); |
||
227 | |||
228 | exit(); |
||
229 | } |
||
230 | } else { |
||
231 | $data[$i]['rowCount'] = $this->container->get('wbm_query_manager.db')->getRowCount($query); |
||
232 | $data[$i]['fetchData'] = null; |
||
233 | } |
||
234 | |||
235 | $i++; |
||
236 | } while ($this->container->get('wbm_query_manager.db')->nextResult($query)); |
||
237 | |||
238 | $this->container->get('wbm_query_manager.db')->close($query); |
||
239 | |||
240 | $this->View()->assign( |
||
241 | array('success' => true, 'data' => array_reverse($data)) |
||
242 | ); |
||
243 | } catch (Exception $e) { |
||
244 | $this->View()->assign( |
||
245 | array('success' => false, 'data' => $e->getMessage()) |
||
246 | ); |
||
247 | } |
||
248 | } |
||
249 | |||
252 |