Completed
Push — master ( 180eae...7546b3 )
by ARCANEDEV
05:31
created

LogViewerController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 98.77%

Importance

Changes 0
Metric Value
dl 0
loc 280
ccs 80
cts 81
cp 0.9877
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 12

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 8 1
A listLogs() 0 8 1
A show() 0 10 1
A showByLevel() 0 12 2
A view() 0 6 1
A paginate() 0 14 1
A getLogOrFail() 0 13 2
A prepareChartData() 0 15 1
A calcPercentages() 0 15 3
A search() 0 16 2
A download() 0 4 1
A delete() 0 10 2
1
<?php namespace Arcanedev\LogViewer\Http\Controllers;
2
3
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract;
4
use Arcanedev\LogViewer\Entities\LogEntry;
5
use Arcanedev\LogViewer\Exceptions\LogNotFoundException;
6
use Arcanedev\LogViewer\Tables\StatsTable;
7
use Illuminate\Http\Request;
8
use Illuminate\Pagination\LengthAwarePaginator;
9
use Illuminate\Routing\Controller;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class     LogViewerController
16
 *
17
 * @package  LogViewer\Http\Controllers
18
 * @author   ARCANEDEV <[email protected]>
19
 */
20
class LogViewerController extends Controller
21
{
22
    /* -----------------------------------------------------------------
23
     |  Properties
24
     | -----------------------------------------------------------------
25
     */
26
27
    /**
28
     * The log viewer instance
29
     *
30
     * @var \Arcanedev\LogViewer\Contracts\LogViewer
31
     */
32
    protected $logViewer;
33
34
    /** @var int */
35
    protected $perPage = 30;
36
37
    /** @var string */
38
    protected $showRoute = 'log-viewer::logs.show';
39
40
    /* -----------------------------------------------------------------
41
     |  Constructor
42
     | -----------------------------------------------------------------
43
     */
44
45
    /**
46
     * LogViewerController constructor.
47
     *
48
     * @param  \Arcanedev\LogViewer\Contracts\LogViewer  $logViewer
49
     */
50 24
    public function __construct(LogViewerContract $logViewer)
51
    {
52 24
        $this->logViewer = $logViewer;
53 24
        $this->perPage = config('log-viewer.per-page', $this->perPage);
54 24
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Main Methods
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Show the dashboard.
63
     *
64
     * @return \Illuminate\View\View
65
     */
66 2
    public function index()
67
    {
68 2
        $stats     = $this->logViewer->statsTable();
69 2
        $chartData = $this->prepareChartData($stats);
70 2
        $percents  = $this->calcPercentages($stats->footer(), $stats->header());
71
72 2
        return $this->view('dashboard', compact('chartData', 'percents'));
73
    }
74
75
    /**
76
     * List all logs.
77
     *
78
     * @param  \Illuminate\Http\Request  $request
79
     *
80
     * @return \Illuminate\View\View
81
     */
82 2
    public function listLogs(Request $request)
83
    {
84 2
        $stats   = $this->logViewer->statsTable();
85 2
        $headers = $stats->header();
86 2
        $rows    = $this->paginate($stats->rows(), $request);
87
88 2
        return $this->view('logs', compact('headers', 'rows'));
89
    }
90
91
    /**
92
     * Show the log.
93
     *
94
     * @param  \Illuminate\Http\Request  $request
95
     * @param  string                    $date
96
     *
97
     * @return \Illuminate\View\View
98
     */
99 4
    public function show(Request $request, $date)
100
    {
101 4
        $level   = 'all';
102 4
        $log     = $this->getLogOrFail($date);
103 2
        $query   = $request->get('query');
104 2
        $levels  = $this->logViewer->levelsNames();
105 2
        $entries = $log->entries($level)->paginate($this->perPage);
106
107 2
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
108
    }
109
110
    /**
111
     * Filter the log entries by level.
112
     *
113
     * @param  \Illuminate\Http\Request  $request
114
     * @param  string                    $date
115
     * @param  string                    $level
116
     *
117
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
118
     */
119 4
    public function showByLevel(Request $request, $date, $level)
120
    {
121 4
        if ($level === 'all')
122 2
            return redirect()->route($this->showRoute, [$date]);
123
124 2
        $log     = $this->getLogOrFail($date);
125 2
        $query   = $request->get('query');
126 2
        $levels  = $this->logViewer->levelsNames();
127 2
        $entries = $this->logViewer->entries($date, $level)->paginate($this->perPage);
128
129 2
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
130
    }
131
132
    /**
133
     * Show the log with the search query.
134
     *
135
     * @param  \Illuminate\Http\Request  $request
136
     * @param  string                    $date
137
     * @param  string                    $level
138
     *
139
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
140
     */
141 4
    public function search(Request $request, $date, $level = 'all')
142
    {
143 4
        $query   = $request->get('query');
144
145 4
        if (is_null($query))
146
            return redirect()->route($this->showRoute, [$date]);
147
148 4
        $log     = $this->getLogOrFail($date);
149 4
        $levels  = $this->logViewer->levelsNames();
150 4
        $needles = explode(' ', $query);
151
        $entries = $log->entries($level)->filter(function ( LogEntry $value) use ($needles) {
152 4
            return Str::containsAll($value->header, $needles);
153 4
        })->paginate($this->perPage);
154
155 4
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
156
    }
157
158
    /**
159
     * Download the log
160
     *
161
     * @param  string  $date
162
     *
163
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
164
     */
165 2
    public function download($date)
166
    {
167 2
        return $this->logViewer->download($date);
168
    }
169
170
    /**
171
     * Delete a log.
172
     *
173
     * @param  \Illuminate\Http\Request  $request
174
     *
175
     * @return \Illuminate\Http\JsonResponse
176
     */
177 6
    public function delete(Request $request)
178
    {
179 6
        abort_unless($request->ajax(), 405, 'Method Not Allowed');
180
181 4
        $date = $request->get('date');
182
183 4
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json 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...
184 4
            'result' => $this->logViewer->delete($date) ? 'success' : 'error'
185
        ]);
186
    }
187
188
    /* -----------------------------------------------------------------
189
     |  Other Methods
190
     | -----------------------------------------------------------------
191
     */
192
193
    /**
194
     * Get the evaluated view contents for the given view.
195
     *
196
     * @param  string  $view
197
     * @param  array   $data
198
     * @param  array   $mergeData
199
     *
200
     * @return \Illuminate\View\View
201
     */
202 12
    protected function view($view, $data = [], $mergeData = [])
203
    {
204 12
        $theme = config('log-viewer.theme');
205
206 12
        return view()->make("log-viewer::{$theme}.{$view}", $data, $mergeData);
0 ignored issues
show
Bug introduced by
The method make does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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...
207
    }
208
209
    /**
210
     * Paginate logs.
211
     *
212
     * @param  array                     $data
213
     * @param  \Illuminate\Http\Request  $request
214
     *
215
     * @return \Illuminate\Pagination\LengthAwarePaginator
216
     */
217 2
    protected function paginate(array $data, Request $request)
218
    {
219 2
        $data = new Collection($data);
220 2
        $page = $request->get('page', 1);
221 2
        $path = $request->url();
222
223 2
        return new LengthAwarePaginator(
224 2
            $data->forPage($page, $this->perPage),
225 2
            $data->count(),
226 2
            $this->perPage,
227 1
            $page,
228 2
            compact('path')
229
        );
230
    }
231
232
    /**
233
     * Get a log or fail
234
     *
235
     * @param  string  $date
236
     *
237
     * @return \Arcanedev\LogViewer\Entities\Log|null
238
     */
239 10
    protected function getLogOrFail($date)
240
    {
241 10
        $log = null;
242
243
        try {
244 10
            $log = $this->logViewer->get($date);
245
        }
246 2
        catch (LogNotFoundException $e) {
247 2
            abort(404, $e->getMessage());
248
        }
249
250 8
        return $log;
251
    }
252
253
    /**
254
     * Prepare chart data.
255
     *
256
     * @param  \Arcanedev\LogViewer\Tables\StatsTable  $stats
257
     *
258
     * @return string
259
     */
260 2
    protected function prepareChartData(StatsTable $stats)
261
    {
262 2
        $totals = $stats->totals()->all();
263
264 2
        return json_encode([
265 2
            'labels'   => Arr::pluck($totals, 'label'),
266
            'datasets' => [
267
                [
268 2
                    'data'                 => Arr::pluck($totals, 'value'),
269 2
                    'backgroundColor'      => Arr::pluck($totals, 'color'),
270 2
                    'hoverBackgroundColor' => Arr::pluck($totals, 'highlight'),
271
                ],
272
            ],
273
        ]);
274
    }
275
276
    /**
277
     * Calculate the percentage.
278
     *
279
     * @param  array  $total
280
     * @param  array  $names
281
     *
282
     * @return array
283
     */
284 2
    protected function calcPercentages(array $total, array $names)
285
    {
286 2
        $percents = [];
287 2
        $all      = Arr::get($total, 'all');
288
289 2
        foreach ($total as $level => $count) {
290 2
            $percents[$level] = [
291 2
                'name'    => $names[$level],
292 2
                'count'   => $count,
293 2
                'percent' => $all ? round(($count / $all) * 100, 2) : 0,
294
            ];
295
        }
296
297 2
        return $percents;
298
    }
299
}
300