| Conditions | 7 |
| Paths | 6 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 33 | public function index(Request $request) |
||
| 34 | { |
||
| 35 | $formData = []; |
||
| 36 | // default |
||
| 37 | $formData['files'] = 'site_'.date('Y-m-d').'.log'; |
||
| 38 | $formData['line_max'] = '50'; |
||
| 39 | |||
| 40 | $builder = $this->formFactory |
||
| 41 | ->createBuilder(LogType::class); |
||
| 42 | |||
| 43 | $event = new EventArgs( |
||
| 44 | [ |
||
| 45 | 'builder' => $builder, |
||
| 46 | 'data' => $formData, |
||
| 47 | ], |
||
| 48 | $request |
||
| 49 | ); |
||
| 50 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_INITIALIZE, $event); |
||
| 51 | $formData = $event->getArgument('data'); |
||
| 52 | |||
| 53 | $form = $builder->getForm(); |
||
| 54 | |||
| 55 | if ('POST' === $request->getMethod()) { |
||
| 56 | $form->handleRequest($request); |
||
| 57 | if ($form->isValid()) { |
||
| 58 | $formData = $form->getData(); |
||
| 59 | } |
||
| 60 | $event = new EventArgs( |
||
| 61 | [ |
||
| 62 | 'form' => $form, |
||
| 63 | ], |
||
| 64 | $request |
||
| 65 | ); |
||
| 66 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_SETTING_SYSTEM_LOG_INDEX_COMPLETE, $event); |
||
| 67 | } |
||
| 68 | $logDir = $this->getParameter('kernel.logs_dir').DIRECTORY_SEPARATOR.$this->getParameter('kernel.environment'); |
||
| 69 | $logFile = $logDir.'/'.$formData['files']; |
||
| 70 | |||
| 71 | if ($form->getClickedButton() && $form->getClickedButton()->getName() === 'download') { |
||
| 72 | $bufferSize = 1024 * 50; |
||
| 73 | $response = new StreamedResponse(); |
||
| 74 | $response->headers->set('Content-Length',filesize($logFile)); |
||
| 75 | $response->headers->set('Content-Disposition','attachment; filename=' . basename($logFile)); |
||
| 76 | $response->headers->set('Content-Type','application/octet-stream'); |
||
| 77 | $response->setCallback(function() use($logFile,$bufferSize) { |
||
| 78 | if ($fh = fopen($logFile,'r')) { |
||
| 79 | while (!feof($fh)) { |
||
| 80 | echo fread($fh,$bufferSize); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | }); |
||
| 84 | $response->send(); |
||
| 85 | return $response; |
||
| 86 | } else { |
||
| 87 | return [ |
||
| 88 | 'form' => $form->createView(), |
||
| 89 | 'log' => $this->parseLogFile($logFile, $formData), |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 122 |