Completed
Push — master ( dbfc09...a50d2e )
by raphael
01:11
created

LogViewerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Rap2hpoutre\Controllers;
4
5
use Illuminate\Support\Facades\Crypt;
6
use Rap2hpoutre\LaravelLogViewer\LaravelLogViewer;
7
8
/**
9
 * Class LogViewerController
10
 * @package Rap2hpoutre\LaravelLogViewer
11
 */
12
class LogViewerController extends BaseController
13
{
14
    /**
15
     * @var
16
     */
17
    protected $request;
18
    /**
19
     * @var LaravelLogViewer
20
     */
21
    private $log_viewer;
22
23
    /**
24
     * @var string
25
     */
26
    protected $view_log = 'laravel-log-viewer::log';
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
        if (is_array($data['logs']) && count($data['logs']) > 0) {
71
            $firstLog = reset($data['logs']);
72
            if (!$firstLog['context'] && !$firstLog['level']) {
73
                $data['standardFormat'] = false;
74
            }
75
        }
76
77
        return app('view')->make($this->view_log, $data);
78
    }
79
80
    /**
81
     * @return bool|mixed
82
     * @throws \Exception
83
     */
84
    private function earlyReturn()
85
    {
86
        if ($this->request->input('f')) {
87
            $this->log_viewer->setFolder(Crypt::decrypt($this->request->input('f')));
88
        }
89
90
        if ($this->request->input('dl')) {
91
            return $this->download($this->pathFromInput('dl'));
92
        } elseif ($this->request->has('clean')) {
93
            app('files')->put($this->pathFromInput('clean'), '');
94
            return $this->redirect($this->request->url());
95
        } elseif ($this->request->has('del')) {
96
            app('files')->delete($this->pathFromInput('del'));
97
            return $this->redirect($this->request->url());
98
        } elseif ($this->request->has('delall')) {
99
            $files = ($this->log_viewer->getFolderName())
100
                        ? $this->log_viewer->getFolderFiles(true)
101
                        : $this->log_viewer->getFiles(true);
102
            foreach ($files as $file) {
103
                app('files')->delete($this->log_viewer->pathToLogFile($file));
104
            }
105
            return $this->redirect($this->request->url());
106
        }
107
        return false;
108
    }
109
110
    /**
111
     * @param string $input_string
112
     * @return string
113
     * @throws \Exception
114
     */
115
    private function pathFromInput($input_string)
116
    {
117
        return $this->log_viewer->pathToLogFile(Crypt::decrypt($this->request->input($input_string)));
118
    }
119
120
    /**
121
     * @param $to
122
     * @return mixed
123
     */
124
    private function redirect($to)
125
    {
126
        if (function_exists('redirect')) {
127
            return redirect($to);
128
        }
129
130
        return app('redirect')->to($to);
131
    }
132
133
    /**
134
     * @param string $data
135
     * @return mixed
136
     */
137
    private function download($data)
138
    {
139
        if (function_exists('response')) {
140
            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...
141
        }
142
143
        // For laravel 4.2
144
        return app('\Illuminate\Support\Facades\Response')->download($data);
145
    }
146
}
147