Completed
Push — master ( 4c8845...0e974a )
by ARCANEDEV
09:55
created

LogViewerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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