Completed
Push — master ( 52c2a3...337bb7 )
by raphael
01:06
created

LogViewerController::redirect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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 \Illuminate\Http\Request
21
     */
22
    protected $request;
23
24
    /**
25
     * @var LaravelLogViewer
26
     */
27
    private $log_viewer;
28
29
    /**
30
     * @var string
31
     */
32
    protected $view_log = 'laravel-log-viewer::log';
33
34
    /**
35
     * LogViewerController constructor.
36
     */
37
    public function __construct()
38
    {
39
        $this->log_viewer = new LaravelLogViewer();
40
        $this->request = app('request');
41
    }
42
43
    /**
44
     * @return array|mixed
45
     * @throws \Exception
46
     */
47
    public function index()
48
    {
49
        $folderFiles = [];
50
        if ($this->request->input('f')) {
51
            $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
0 ignored issues
show
Bug introduced by
It seems like $this->request->input('f') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Support\Facades\Crypt::decrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
52
            $folderFiles = $this->log_viewer->getFolderFiles(true);
53
        }
54
        if ($this->request->input('l')) {
55
            $this->log_viewer->setFile(Crypt::decrypt($this->request->input('l')));
0 ignored issues
show
Bug introduced by
It seems like $this->request->input('l') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Support\Facades\Crypt::decrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
        }
57
58
        if ($early_return = $this->earlyReturn()) {
59
            return $early_return;
60
        }
61
62
        $data = [
63
            'logs' => $this->log_viewer->all(),
64
            'folders' => $this->log_viewer->getFolders(),
65
            'current_folder' => $this->log_viewer->getFolderName(),
66
            'folder_files' => $folderFiles,
67
            'files' => $this->log_viewer->getFiles(true),
68
            'current_file' => $this->log_viewer->getFileName(),
69
            'standardFormat' => true,
70
        ];
71
72
        if ($this->request->wantsJson()) {
73
            return $data;
74
        }
75
76
        if (is_array($data['logs']) && count($data['logs']) > 0) {
77
            $firstLog = reset($data['logs']);
78
            if (!$firstLog['context'] && !$firstLog['level']) {
79
                $data['standardFormat'] = false;
80
            }
81
        }
82
83
        return app('view')->make($this->view_log, $data);
84
    }
85
86
    /**
87
     * @return bool|mixed
88
     * @throws \Exception
89
     */
90
    private function earlyReturn()
91
    {
92
        if ($this->request->input('f')) {
93
            $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
0 ignored issues
show
Bug introduced by
It seems like $this->request->input('f') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Illuminate\Support\Facades\Crypt::decrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
94
        }
95
96
        if ($this->request->input('dl')) {
97
            return $this->download($this->pathFromInput('dl'));
98
        } elseif ($this->request->has('clean')) {
99
            app('files')->put($this->pathFromInput('clean'), '');
100
            return $this->redirect(url()->previous());
101
        } elseif ($this->request->has('del')) {
102
            app('files')->delete($this->pathFromInput('del'));
103
            return $this->redirect($this->request->url());
104
        } elseif ($this->request->has('delall')) {
105
            $files = ($this->log_viewer->getFolderName())
106
                        ? $this->log_viewer->getFolderFiles(true)
107
                        : $this->log_viewer->getFiles(true);
108
            foreach ($files as $file) {
109
                app('files')->delete($this->log_viewer->pathToLogFile($file));
110
            }
111
            return $this->redirect($this->request->url());
112
        }
113
        return false;
114
    }
115
116
    /**
117
     * @param string $input_string
118
     * @return string
119
     * @throws \Exception
120
     */
121
    private function pathFromInput($input_string)
122
    {
123
        return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
0 ignored issues
show
Bug introduced by
It seems like $this->request->input($input_string) targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array or null; however, Illuminate\Support\Facades\Crypt::decrypt() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
124
    }
125
126
    /**
127
     * @param $to
128
     * @return mixed
129
     */
130
    private function redirect($to)
131
    {
132
        if (function_exists('redirect')) {
133
            return redirect($to);
134
        }
135
136
        return app('redirect')->to($to);
137
    }
138
139
    /**
140
     * @param string $data
141
     * @return mixed
142
     */
143
    private function download($data)
144
    {
145
        if (function_exists('response')) {
146
            return response()->download($data);
0 ignored issues
show
Bug introduced by
The method download does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
147
        }
148
149
        // For laravel 4.2
150
        return app('\Illuminate\Support\Facades\Response')->download($data);
151
    }
152
}
153