Completed
Push — feature/customize-username ( bb12d9...1746f4 )
by Kiyotaka
04:30
created

LogController::index()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 1
dl 0
loc 60
rs 7.9393
c 0
b 0
f 0

How to fix   Long Method   

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
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Controller\Admin\Setting\System;
15
16
use Eccube\Controller\AbstractController;
17
use Eccube\Event\EccubeEvents;
18
use Eccube\Event\EventArgs;
19
use Eccube\Form\Type\Admin\LogType;
20
use Symfony\Component\Routing\Annotation\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\StreamedResponse;
24
25
class LogController extends AbstractController
26
{
27
    /**
28
     * @Route("/%eccube_admin_route%/setting/system/log", name="admin_setting_system_log")
29
     * @Template("@admin/Setting/System/log.twig")
30
     *
31
     * @return array|Symfony\Component\HttpFoundation\StreamedResponse
32
     */
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
94
    /**
95
     * parse log file
96
     *
97
     * @param string $logFile
98
     * @param $formData
99
     *
100
     * @return array
101
     */
102
    private function parseLogFile($logFile, $formData)
103
    {
104
        $log = [];
105
106
        if (!file_exists($logFile)) {
107
            return $log;
108
        }
109
110
        foreach (array_reverse(file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)) as $line) {
111
            // 上限に達した場合、処理を抜ける
112
            if (count($log) >= $formData['line_max']) {
113
                break;
114
            }
115
116
            $log[] = $line;
117
        }
118
119
        return $log;
120
    }
121
}
122