Completed
Push — master ( a8cd76...ec5938 )
by ARCANEDEV
76:11 queued 35:04
created

LogViewerController::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 15
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 9.7666
c 0
b 0
f 0
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 33
    public function __construct(LogViewerContract $logViewer)
51
    {
52 33
        $this->logViewer = $logViewer;
53 33
        $this->perPage = config('log-viewer.per-page', $this->perPage);
54 33
    }
55
56
    /* -----------------------------------------------------------------
57
     |  Main Methods
58
     | -----------------------------------------------------------------
59
     */
60
61
    /**
62
     * Show the dashboard.
63
     *
64
     * @return \Illuminate\View\View
65
     */
66 3
    public function index()
67
    {
68 3
        $stats     = $this->logViewer->statsTable();
69 3
        $chartData = $this->prepareChartData($stats);
70 3
        $percents  = $this->calcPercentages($stats->footer(), $stats->header());
71
72 3
        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 3
    public function listLogs(Request $request)
83
    {
84 3
        $stats   = $this->logViewer->statsTable();
85 3
        $headers = $stats->header();
86 3
        $rows    = $this->paginate($stats->rows(), $request);
87
88 3
        return $this->view('logs', compact('headers', 'rows'));
89
    }
90
91
    /**
92
     * Show the log.
93
     *
94
     * @param  string                    $date
95
     * @param  \Illuminate\Http\Request  $request
96
     *
97
     * @return \Illuminate\View\View
98
     */
99 6
    public function show($date, Request $request)
100
    {
101 6
        $level   = 'all';
102 6
        $log     = $this->getLogOrFail($date);
103 3
        $query   = $request->get('query');
104 3
        $levels  = $this->logViewer->levelsNames();
105 3
        $entries = $log->entries($level)->paginate($this->perPage);
106
107 3
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
108
    }
109
110
    /**
111
     * Filter the log entries by level.
112
     *
113
     * @param  string                    $date
114
     * @param  string                    $level
115
     * @param  \Illuminate\Http\Request  $request
116
     *
117
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
118
     */
119 6
    public function showByLevel($date, $level, Request $request)
120
    {
121 6
        if ($level === 'all')
122 3
            return redirect()->route($this->showRoute, [$date]);
123
124 3
        $log     = $this->getLogOrFail($date);
125 3
        $query   = $request->get('query');
126 3
        $levels  = $this->logViewer->levelsNames();
127 3
        $entries = $this->logViewer->entries($date, $level)->paginate($this->perPage);
128
129 3
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
130
    }
131
132
    /**
133
     * Show the log with the search query.
134
     *
135
     * @param  string                    $date
136
     * @param  string                    $level
137
     * @param  \Illuminate\Http\Request  $request
138
     *
139
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
140
     */
141 3
    public function search($date, $level = 'all', Request $request)
142
    {
143 3
        $query   = $request->get('query');
144
145 3
        if (is_null($query))
146
            return redirect()->route($this->showRoute, [$date]);
147
148 3
        $log     = $this->getLogOrFail($date);
149 3
        $levels  = $this->logViewer->levelsNames();
150
        $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) {
151 3
            return Str::contains($value->header, $query);
152 3
        })->paginate($this->perPage);
153
154 3
        return $this->view('show', compact('level', 'log', 'query', 'levels', 'entries'));
155
    }
156
157
    /**
158
     * Download the log
159
     *
160
     * @param  string  $date
161
     *
162
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
163
     */
164 3
    public function download($date)
165
    {
166 3
        return $this->logViewer->download($date);
167
    }
168
169
    /**
170
     * Delete a log.
171
     *
172
     * @param  \Illuminate\Http\Request  $request
173
     *
174
     * @return \Illuminate\Http\JsonResponse
175
     */
176 9
    public function delete(Request $request)
177
    {
178 9
        if ( ! $request->ajax())
179 3
            abort(405, 'Method Not Allowed');
180
181 6
        $date = $request->get('date');
182
183 6
        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 6
            '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 15
    protected function view($view, $data = [], $mergeData = [])
203
    {
204 15
        $theme = config('log-viewer.theme');
205
206 15
        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 3
    protected function paginate(array $data, Request $request)
218
    {
219 3
        $data = new Collection($data);
220 3
        $page = $request->get('page', 1);
221 3
        $path = $request->url();
222
223 3
        return new LengthAwarePaginator(
224 3
            $data->forPage($page, $this->perPage),
225 3
            $data->count(),
226 3
            $this->perPage,
227 2
            $page,
228 3
            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 12
    protected function getLogOrFail($date)
240
    {
241 12
        $log = null;
242
243
        try {
244 12
            $log = $this->logViewer->get($date);
245
        }
246 3
        catch (LogNotFoundException $e) {
247 3
            abort(404, $e->getMessage());
248
        }
249
250 9
        return $log;
251
    }
252
253
    /**
254
     * Prepare chart data.
255
     *
256
     * @param  \Arcanedev\LogViewer\Tables\StatsTable  $stats
257
     *
258
     * @return string
259
     */
260 3
    protected function prepareChartData(StatsTable $stats)
261
    {
262 3
        $totals = $stats->totals()->all();
263
264 3
        return json_encode([
265 3
            'labels'   => Arr::pluck($totals, 'label'),
266
            'datasets' => [
267
                [
268 3
                    'data'                 => Arr::pluck($totals, 'value'),
269 3
                    'backgroundColor'      => Arr::pluck($totals, 'color'),
270 3
                    '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 3
    protected function calcPercentages(array $total, array $names)
285
    {
286 3
        $percents = [];
287 3
        $all      = Arr::get($total, 'all');
288
289 3
        foreach ($total as $level => $count) {
290 3
            $percents[$level] = [
291 3
                'name'    => $names[$level],
292 3
                'count'   => $count,
293 3
                'percent' => $all ? round(($count / $all) * 100, 2) : 0,
294
            ];
295
        }
296
297 3
        return $percents;
298
    }
299
}
300