Completed
Push — master ( 41b91a...4502f4 )
by raphael
02:52
created

LogViewerController::earlyReturn()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
cc 8
nc 16
nop 0
1
<?php
2
3
namespace Rap2hpoutre\LaravelLogViewer;
4
5
use Illuminate\Support\Facades\Crypt;
6
7
if (class_exists("\\Illuminate\\Routing\\Controller")) {
8
    class BaseController extends \Illuminate\Routing\Controller {}
9
} elseif (class_exists("Laravel\\Lumen\\Routing\\Controller")) {
10
    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 (L8-8) 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...
11
}
12
13
/**
14
 * Class LogViewerController
15
 * @package Rap2hpoutre\LaravelLogViewer
16
 */
17
class LogViewerController extends BaseController
18
{
19
    /**
20
     * @var
21
     */
22
    protected $request;
23
    /**
24
     * @var LaravelLogViewer
25
     */
26
    private $log_viewer;
27
28
    /**
29
     * LogViewerController constructor.
30
     */
31
    public function __construct()
32
    {
33
        $this->log_viewer = new LaravelLogViewer();
34
        $this->request = app('request');
35
    }
36
37
    /**
38
     * @return array|mixed
39
     * @throws \Exception
40
     */
41
    public function index()
42
    {
43
        $folderFiles = [];
44
        if ($this->request->input('f')) {
45
            $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
46
            $folderFiles = $this->log_viewer->getFolderFiles(true);
47
        }
48
        if ($this->request->input('l')) {
49
            $this->log_viewer->setFile(Crypt::decrypt($this->request->input('l')));
50
        }
51
52
        if ($early_return = $this->earlyReturn()) {
53
            return $early_return;
54
        }
55
56
        $data = [
57
            'logs' => $this->log_viewer->all(),
58
            'folders' => $this->log_viewer->getFolders(),
59
            'current_folder' => $this->log_viewer->getFolderName(),
60
            'folder_files' => $folderFiles,
61
            'files' => $this->log_viewer->getFiles(true),
62
            'current_file' => $this->log_viewer->getFileName(),
63
            'standardFormat' => true,
64
        ];
65
66
        if ($this->request->wantsJson()) {
67
            return $data;
68
        }
69
70
        $firstLog = reset($data['logs']);
71
        if (!$firstLog['context'] && !$firstLog['level']) {
72
            $data['standardFormat'] = false;
73
        }
74
75
        return app('view')->make('laravel-log-viewer::log', $data);
76
    }
77
78
    /**
79
     * @return bool|mixed
80
     * @throws \Exception
81
     */
82
    private function earlyReturn()
83
    {
84
        if ($this->request->input('f')) {
85
            $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
86
        }
87
88
        if ($this->request->input('dl')) {
89
            return $this->download($this->pathFromInput('dl'));
90
        } elseif ($this->request->has('clean')) {
91
            app('files')->put($this->pathFromInput('clean'), '');
92
            return $this->redirect($this->request->url());
93
        } elseif ($this->request->has('del')) {
94
            app('files')->delete($this->pathFromInput('del'));
95
            return $this->redirect($this->request->url());
96
        } elseif ($this->request->has('delall')) {
97
            $files = ($this->log_viewer->getFolderName())
98
                        ? $this->log_viewer->getFolderFiles(true)
99
                        : $this->log_viewer->getFiles(true);
100
            foreach ($files as $file) {
101
                app('files')->delete($this->log_viewer->pathToLogFile($file));
102
            }
103
            return $this->redirect($this->request->url());
104
        }
105
        return false;
106
    }
107
108
    /**
109
     * @param string $input_string
110
     * @return string
111
     * @throws \Exception
112
     */
113
    private function pathFromInput($input_string)
114
    {
115
        return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
116
    }
117
118
    /**
119
     * @param $to
120
     * @return mixed
121
     */
122
    private function redirect($to)
123
    {
124
        if (function_exists('redirect')) {
125
            return redirect($to);
126
        }
127
128
        return app('redirect')->to($to);
129
    }
130
131
    /**
132
     * @param string $data
133
     * @return mixed
134
     */
135
    private function download($data)
136
    {
137
        if (function_exists('response')) {
138
            return response()->download($data);
139
        }
140
141
        // For laravel 4.2
142
        return app('\Illuminate\Support\Facades\Response')->download($data);
143
    }
144
}
145