Completed
Push — master ( aad7c0...f944cb )
by raphael
05:54 queued 02:21
created

LogViewerController::earlyReturn()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.0444
cc 6
nc 6
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
} elseif (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
/**
12
 * Class LogViewerController
13
 * @package Rap2hpoutre\LaravelLogViewer
14
 */
15
class LogViewerController extends BaseController
16
{
17
    protected $request;
18
19
    /**
20
     * LogViewerController constructor.
21
     */
22
    public function __construct ()
23
    {
24
        $this->request = app('request');
25
    }
26
27
    /**
28
     * @return array|mixed
29
     * @throws \Exception
30
     */
31
    public function index()
32
    {
33
        if ($this->request->input('l')) {
34
            LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l')));
35
        }
36
37
        if ($early_return = $this->earlyReturn()) {
38
            return $early_return;
39
        }
40
        
41
        $data = [
42
            'logs' => LaravelLogViewer::all(),
43
            'files' => LaravelLogViewer::getFiles(true),
44
            'current_file' => LaravelLogViewer::getFileName(),
45
            'standardFormat' => true,
46
        ];
47
48
        if ($this->request->wantsJson()) {
49
            return $data;
50
        }
51
52
        $firstLog = reset($data['logs']);
53
        if (!$firstLog['context'] && !$firstLog['level']) {
54
            $data['standardFormat'] = false;
55
        }
56
57
        return app('view')->make('laravel-log-viewer::log', $data);
58
    }
59
60
    /**
61
     * @return bool|mixed
62
     * @throws \Exception
63
     */
64
    private function earlyReturn()
65
    {
66
        if ($this->request->input('dl')) {
67
            return $this->download($this->pathFromInput('dl'));
68
        } elseif ($this->request->has('clean')) {
69
            app('files')->put($this->pathFromInput('clean'), '');
70
            return $this->redirect($this->request->url());
71
        } elseif ($this->request->has('del')) {
72
            app('files')->delete($this->pathFromInput('del'));
73
            return $this->redirect($this->request->url());
74
        } elseif ($this->request->has('delall')) {
75
            foreach(LaravelLogViewer::getFiles(true) as $file){
76
                app('files')->delete(LaravelLogViewer::pathToLogFile($file));
77
            }
78
            return $this->redirect($this->request->url());
79
        }
80
        return false;
81
    }
82
83
    /**
84
     * @param $input_string
85
     * @return string
86
     * @throws \Exception
87
     */
88
    private function pathFromInput($input_string)
89
    {
90
        return LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
91
    }
92
93
    /**
94
     * @param $to
95
     * @return mixed
96
     */
97
    private function redirect($to)
98
    {
99
        if (function_exists('redirect')) {
100
            return redirect($to);
101
        }
102
103
        return app('redirect')->to($to);
104
    }
105
106
    /**
107
     * @param string $data
108
     * @return mixed
109
     */
110
    private function download($data)
111
    {
112
        if (function_exists('response')) {
113
            return response()->download($data);
114
        }
115
116
        // For laravel 4.2
117
        return app('\Illuminate\Support\Facades\Response')->download($data);
118
    }
119
}
120