Completed
Push — master ( 86f9de...4ee68b )
by raphael
03:34 queued 02:05
created

LogViewerController::pathFromInput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
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
/**
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
34
        if ($this->request->input('l')) {
35
            LaravelLogViewer::setFile(Crypt::decrypt($this->request->input('l')));
36
        }
37
38
        if ($this->request->input('dl')) {
39
            return $this->download($this->pathFromInput('dl'));
40
        } elseif ($this->request->has('clean')) {
41
            app('files')->put($this->pathFromInput('clean'), '');
42
            return $this->redirect($this->request->url());
43
        } elseif ($this->request->has('del')) {
44
            app('files')->delete($this->pathFromInput('del'));
45
            return $this->redirect($this->request->url());
46
        } elseif ($this->request->has('delall')) {
47
            foreach(LaravelLogViewer::getFiles(true) as $file){
48
                app('files')->delete(LaravelLogViewer::pathToLogFile($file));
49
            }
50
            return $this->redirect($this->request->url());
51
        }
52
        
53
        $data = [
54
            'logs' => LaravelLogViewer::all(),
55
            'files' => LaravelLogViewer::getFiles(true),
56
            'current_file' => LaravelLogViewer::getFileName(),
57
            'standardFormat' => true,
58
        ];
59
60
        if ($this->request->wantsJson()) {
61
            return $data;
62
        }
63
64
        $firstLog = reset($data['logs']);
65
        if (!$firstLog['context'] && !$firstLog['level']) {
66
            $data['standardFormat'] = false;
67
        }
68
69
        return app('view')->make('laravel-log-viewer::log', $data);
70
    }
71
72
    /**
73
     * @param $input_string
74
     * @return string
75
     * @throws \Exception
76
     */
77
    private function pathFromInput($input_string)
78
    {
79
        return LaravelLogViewer::pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
80
    }
81
82
    /**
83
     * @param $to
84
     * @return mixed
85
     */
86
    private function redirect($to)
87
    {
88
        if (function_exists('redirect')) {
89
            return redirect($to);
90
        }
91
92
        return app('redirect')->to($to);
93
    }
94
95
    /**
96
     * @param string $data
97
     * @return mixed
98
     */
99
    private function download($data)
100
    {
101
        if (function_exists('response')) {
102
            return response()->download($data);
103
        }
104
105
        // For laravel 4.2
106
        return app('\Illuminate\Support\Facades\Response')->download($data);
107
    }
108
}
109