Completed
Pull Request — master (#65)
by
unknown
03:04
created

LogViewerController::index()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 1 Features 2
Metric Value
c 13
b 1
f 2
dl 0
loc 25
rs 5.3846
cc 8
eloc 16
nc 6
nop 0
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
4
use Illuminate\Routing\Controller;
5
use Illuminate\Support\Facades\File;
6
use Illuminate\Support\Facades\View;
7
use Illuminate\Support\Facades\Redirect;
8
use Illuminate\Support\Facades\Request;
9
use Illuminate\Support\Facades\Response;
10
11
class LogViewerController extends Controller
12
{
13
    /**
14
     * Config filename.
15
     * @var string
16
     */
17
    protected static $configFile = 'laravel-log-viewer';
18
19
    public function index()
20
    {
21
22
        if (Request::input('l')) {
23
            LaravelLogViewer::setFile(base64_decode(Request::input('l')));
24
        }
25
26
        if (Request::input('dl')) {
27
            return Response::download(LaravelLogViewer::pathToLogFile(base64_decode(Request::input('dl'))));
28
        } elseif (Request::has('del')) {
29
            File::delete(LaravelLogViewer::pathToLogFile(base64_decode(Request::input('del'))));
30
            return Redirect::to(Request::url());
31
        }
32
33
        $logs = LaravelLogViewer::all();
34
35
        return View::make(self::cfgHas('view.log') ? self::cfg('view.log') : 'laravel-log-viewer::log', [
36
            'layout' => self::cfgHas('view.layout') ? self::cfg('view.layout') : 'laravel-log-viewer::layout',
37
            'yieldName' => self::cfgHas('view.yieldName') ? self::cfg('view.yieldName') : 'content',
38
            'container_fluid' => self::cfgHas('view.container-fluid') ? self::cfg('view.container-fluid') : true,
39
            'logs' => $logs,
40
            'files' => LaravelLogViewer::getFiles(true),
41
            'current_file' => LaravelLogViewer::getFileName()
42
        ]);
43
    }
44
45
    /**
46
     * Retrieve a configuration value for the log viewer.
47
     * @param $key
48
     * @return mixed
49
     */
50
    private static function cfg($key)
51
    {
52
        return config()->get(self::$configFile . '.' . $key);
53
    }
54
55
    /**
56
     * Check if a configuration value for the log viewer exists.
57
     * @param $key
58
     * @return mixed
59
     */
60
    private static function cfgHas($key)
61
    {
62
        return config()->has(self::$configFile . '.' . $key);
63
    }
64
}
65