Completed
Push — master ( 1e3900...0d4e38 )
by raphael
01:20
created

LogViewerController::index()   D

Complexity

Conditions 9
Paths 14

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 37
rs 4.909
cc 9
eloc 23
nc 14
nop 0
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
use Illuminate\Support\Facades\Crypt;
4
5
if (class_exists("\\Illuminate\\Routing\\Controller")) {
6
    class BaseController extends \Illuminate\Routing\Controller {}
7
} else if (class_exists("Laravel\\Lumen\\Routing\\Controller")) {
8
    class BaseController extends \Laravel\Lumen\Routing\Controller {}
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Rap2hpoutre\LaravelLogViewer\BaseController has been defined more than once; this definition is ignored, only the first definition in this file (L6-6) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
9
}
10
11
class LogViewerController extends BaseController
12
{
13
    protected $request;
14
15
    public function __construct ()
16
    {
17
        $this->request = app('request');
18
    }
19
20
    public function index()
21
    {
22
23
        if ($this->request->input('l')) {
24
            LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l')));
25
        }
26
27
        if ($this->request->input('dl')) {
28
            return $this->download(LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input('dl'))));
29
        } elseif ($this->request->has('del')) {
30
            app('files')->delete(LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input('del'))));
31
            return $this->redirect($this->request->url());
32
        } elseif ($this->request->has('delall')) {
33
            foreach(LaravelLogViewer::getFiles(true) as $file){
34
                app('files')->delete(LaravelLogViewer::pathToLogFile($file));
35
            }
36
            return $this->redirect($this->request->url());
37
        }
38
        
39
        $data = [
40
            'logs' => LaravelLogViewer::all(),
41
            'files' => LaravelLogViewer::getFiles(true),
42
            'current_file' => LaravelLogViewer::getFileName(),
43
            'standardFormat' => true,
44
        ];
45
46
        if ($this->request->wantsJson()) {
47
            return $data;
48
        }
49
50
        $firstLog = reset($data['logs']);
51
        if (!$firstLog['context'] && !$firstLog['level']) {
52
            $data['standardFormat'] = false;
53
        }
54
55
        return app('view')->make('laravel-log-viewer::log', $data);
56
    }
57
58
    private function redirect($to)
59
    {
60
        if (function_exists('redirect')) {
61
            return redirect($to);
62
        }
63
64
        return app('redirect')->to($to);
65
    }
66
67
    private function download($data)
68
    {
69
        if (function_exists('response')) {
70
            return response()->download($data);
71
        }
72
73
        // For laravel 4.2
74
        return app('\Illuminate\Support\Facades\Response')->download($data);
75
    }
76
}
77