Completed
Pull Request — master (#154)
by ARCANEDEV
08:14
created

LogViewerController::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 9.4285
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\Str;
12
13
/**
14
 * Class     LogViewerController
15
 *
16
 * @package  LogViewer\Http\Controllers
17
 * @author   ARCANEDEV <[email protected]>
18
 */
19
class LogViewerController extends Controller
20
{
21
    /* -----------------------------------------------------------------
22
     |  Properties
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * The log viewer instance
28
     *
29
     * @var \Arcanedev\LogViewer\Contracts\LogViewer
30
     */
31
    protected $logViewer;
32
33
    /** @var int */
34
    protected $perPage = 30;
35
36
    /** @var string */
37
    protected $showRoute = 'log-viewer::logs.show';
38
39
    /* -----------------------------------------------------------------
40
     |  Constructor
41
     | -----------------------------------------------------------------
42
     */
43
44
    /**
45
     * LogViewerController constructor.
46
     *
47
     * @param  \Arcanedev\LogViewer\Contracts\LogViewer  $logViewer
48
     */
49 30
    public function __construct(LogViewerContract $logViewer)
50
    {
51 30
        $this->logViewer = $logViewer;
52 30
        $this->perPage = config('log-viewer.per-page', $this->perPage);
53 30
    }
54
55
    /* -----------------------------------------------------------------
56
     |  Main Methods
57
     | -----------------------------------------------------------------
58
     */
59
60
    /**
61
     * Show the dashboard.
62
     *
63
     * @return \Illuminate\View\View
64
     */
65 3
    public function index()
66
    {
67 3
        $stats     = $this->logViewer->statsTable();
68 3
        $chartData = $this->prepareChartData($stats);
69 3
        $percents  = $this->calcPercentages($stats->footer(), $stats->header());
70
71 3
        return $this->view('dashboard', compact('chartData', 'percents'));
72
    }
73
74
    /**
75
     * List all logs.
76
     *
77
     * @param  \Illuminate\Http\Request  $request
78
     *
79
     * @return \Illuminate\View\View
80
     */
81 4
    public function listLogs(Request $request)
82
    {
83 4
        $stats   = $this->logViewer->statsTable();
84 3
        $headers = $stats->header();
85 3
        $rows    = $this->paginate($stats->rows(), $request);
86
87 3
        return $this->view('logs', compact('headers', 'rows', 'footer'));
88
    }
89
90
    /**
91
     * Show the log.
92
     *
93
     * @param  string  $date
94
     *
95
     * @return \Illuminate\View\View
96
     */
97 6
    public function show($date)
98
    {
99 6
        $log     = $this->getLogOrFail($date);
100 3
        $levels  = $this->logViewer->levelsNames();
101 3
        $entries = $log->entries($level = 'all')->paginate($this->perPage);
102
103 3
        return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries'));
104
    }
105
106
    /**
107
     * Filter the log entries by level.
108
     *
109
     * @param  string  $date
110
     * @param  string  $level
111
     *
112
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
113
     */
114 6
    public function showByLevel($date, $level)
115
    {
116 6
        $log = $this->getLogOrFail($date);
117
118 6
        if ($level === 'all')
119 4
            return redirect()->route($this->showRoute, [$date]);
120
121 3
        $levels  = $this->logViewer->levelsNames();
122 3
        $entries = $this->logViewer->entries($date, $level)->paginate($this->perPage);
123
124 3
        return $this->view('show', compact('log', 'levels', 'level', 'search', 'entries'));
125
    }
126
127
    /**
128
     * Show the log with the search query.
129
     *
130
     * @param  string                    $date
131
     * @param  string                    $level
132
     * @param  \Illuminate\Http\Request  $request
133
     *
134
     * @return \Illuminate\View\View
135
     */
136
    public function search($date, $level = 'all', Request $request) {
137
        $log   = $this->getLogOrFail($date);
138
139
        if (is_null($query = $request->get('query')))
140
            return redirect()->route('log-viewer::logs.show', [$date]);
141
142
        $levels  = $this->logViewer->levelsNames();
143
        $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) {
144
            return Str::contains($value->header, $query);
145
        })->paginate($this->perPage);
146
147
        return $this->view('show', compact('log', 'levels', 'level', 'query', 'entries'));
148
    }
149
150
    /**
151
     * Download the log
152
     *
153
     * @param  string  $date
154
     *
155
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
156
     */
157 3
    public function download($date)
158
    {
159 3
        return $this->logViewer->download($date);
160
    }
161
162
    /**
163
     * Delete a log.
164
     *
165
     * @param  \Illuminate\Http\Request  $request
166
     *
167
     * @return \Illuminate\Http\JsonResponse
168
     */
169 9
    public function delete(Request $request)
170
    {
171 9
        if ( ! $request->ajax())
172 5
            abort(405, 'Method Not Allowed');
173
174 6
        $date = $request->get('date');
175
176 6
        return response()->json([
177 6
            'result' => $this->logViewer->delete($date) ? 'success' : 'error'
178 1
        ]);
179
    }
180
181
    /* -----------------------------------------------------------------
182
     |  Other Methods
183
     | -----------------------------------------------------------------
184
     */
185
186
    /**
187
     * Get the evaluated view contents for the given view.
188
     *
189
     * @param  string  $view
190
     * @param  array   $data
191
     * @param  array   $mergeData
192
     *
193
     * @return \Illuminate\View\View
194
     */
195 12
    protected function view($view, $data = [], $mergeData = [])
196
    {
197 12
        return view('log-viewer::'.$view, $data, $mergeData);
198
    }
199
200
    /**
201
     * Paginate logs.
202
     *
203
     * @param  array                     $data
204
     * @param  \Illuminate\Http\Request  $request
205
     *
206
     * @return \Illuminate\Pagination\LengthAwarePaginator
207
     */
208 3
    protected function paginate(array $data, Request $request)
209
    {
210 3
        $data = collect($data);
211 3
        $page = $request->get('page', 1);
212 3
        $url  = $request->url();
213
214 3
        return new LengthAwarePaginator(
215 3
            $data->forPage($page, $this->perPage),
216 3
            $data->count(),
217 3
            $this->perPage,
218 3
            $page,
219 3
            compact('url')
220 1
        );
221
    }
222
223
    /**
224
     * Get a log or fail
225
     *
226
     * @param  string  $date
227
     *
228
     * @return \Arcanedev\LogViewer\Entities\Log|null
229
     */
230 12
    protected function getLogOrFail($date)
231
    {
232 12
        $log = null;
233
234
        try {
235 12
            $log = $this->logViewer->get($date);
236
        }
237 6
        catch (LogNotFoundException $e) {
238 3
            abort(404, $e->getMessage());
239
        }
240
241 9
        return $log;
242
    }
243
244
    /**
245
     * Prepare chart data.
246
     *
247
     * @param  \Arcanedev\LogViewer\Tables\StatsTable  $stats
248
     *
249
     * @return string
250
     */
251 3
    protected function prepareChartData(StatsTable $stats)
252
    {
253 3
        $totals = $stats->totals()->all();
254
255 3
        return json_encode([
256 3
            'labels'   => Arr::pluck($totals, 'label'),
257
            'datasets' => [
258
                [
259 3
                    'data'                 => Arr::pluck($totals, 'value'),
260 3
                    'backgroundColor'      => Arr::pluck($totals, 'color'),
261 3
                    'hoverBackgroundColor' => Arr::pluck($totals, 'highlight'),
262 1
                ],
263 1
            ],
264 1
        ]);
265
    }
266
267
    /**
268
     * Calculate the percentage.
269
     *
270
     * @param  array  $total
271
     * @param  array  $names
272
     *
273
     * @return array
274
     */
275 3
    protected function calcPercentages(array $total, array $names)
276
    {
277 3
        $percents = [];
278 3
        $all      = Arr::get($total, 'all');
279
280 3
        foreach ($total as $level => $count) {
281 3
            $percents[$level] = [
282 3
                'name'    => $names[$level],
283 3
                'count'   => $count,
284 3
                'percent' => $all ? round(($count / $all) * 100, 2) : 0,
285
            ];
286 1
        }
287
288 3
        return $percents;
289
    }
290
}
291