Completed
Push — master ( 9176a9...025952 )
by raphael
01:21
created

LogViewerController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
c 4
b 0
f 0
lcom 1
cbo 2
dl 0
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C index() 0 31 7
A redirect() 0 8 2
A download() 0 9 2
1
<?php
2
namespace Rap2hpoutre\LaravelLogViewer;
3
4
if (class_exists("\\Illuminate\\Routing\\Controller")) {
5
    class BaseController extends \Illuminate\Routing\Controller {}
6
} else if (class_exists("Laravel\\Lumen\\Routing\\Controller")) {
7
    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 (L5-5) 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...
8
}
9
10
class LogViewerController extends BaseController
11
{
12
    protected $request;
13
14
    public function __construct ()
15
    {
16
        $this->request = app('request');
17
    }
18
19
    public function index()
20
    {
21
22
        if ($this->request->input('l')) {
23
            LaravelLogViewer::setFile(base64_decode($this->request->input('l')));
24
        }
25
26
        if ($this->request->input('dl')) {
27
            return $this->download(LaravelLogViewer::pathToLogFile(base64_decode($this->request->input('dl'))));
28
        } elseif ($this->request->has('del')) {
29
            app('files')->delete(LaravelLogViewer::pathToLogFile(base64_decode($this->request->input('del'))));
30
            return $this->redirect($this->request->url());
31
        } elseif ($this->request->has('delall')) {
32
            foreach(LaravelLogViewer::getFiles(true) as $file){
33
                app('files')->delete(LaravelLogViewer::pathToLogFile($file));
34
            }
35
            return $this->redirect($this->request->url());
36
        }
37
        
38
        $data = [
39
            'logs' => LaravelLogViewer::all(),
40
            'files' => LaravelLogViewer::getFiles(true),
41
            'current_file' => LaravelLogViewer::getFileName()
42
        ];
43
44
        if ($this->request->wantsJson()) {
45
            return $data;
46
        }
47
48
        return app('view')->make('laravel-log-viewer::log', $data);
49
    }
50
51
    private function redirect($to)
52
    {
53
        if (function_exists('redirect')) {
54
            return redirect($to);
55
        }
56
57
        return app('redirect')->to($to);
58
    }
59
60
    private function download($data)
61
    {
62
        if (function_exists('response')) {
63
            return response()->download($data);
64
        }
65
66
        // For laravel 4.2
67
        return app('\Illuminate\Support\Facades\Response')->download($data);
68
    }
69
}
70