1 | <?php namespace Arcanedev\LogViewer\Http\Controllers; |
||
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 | 22 | public function __construct(LogViewerContract $logViewer) |
|
54 | |||
55 | /* ----------------------------------------------------------------- |
||
56 | | Main Methods |
||
57 | | ----------------------------------------------------------------- |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * Show the dashboard. |
||
62 | * |
||
63 | * @return \Illuminate\View\View |
||
64 | */ |
||
65 | 2 | public function index() |
|
73 | |||
74 | /** |
||
75 | * List all logs. |
||
76 | * |
||
77 | * @param \Illuminate\Http\Request $request |
||
78 | * |
||
79 | * @return \Illuminate\View\View |
||
80 | */ |
||
81 | 2 | public function listLogs(Request $request) |
|
89 | |||
90 | /** |
||
91 | * Show the log. |
||
92 | * |
||
93 | * @param string $date |
||
94 | * |
||
95 | * @return \Illuminate\View\View |
||
96 | */ |
||
97 | 4 | public function show($date) |
|
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 | 4 | public function showByLevel($date, $level) |
|
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 | 2 | public function search($date, $level = 'all', Request $request) { |
|
137 | 2 | $log = $this->getLogOrFail($date); |
|
138 | |||
139 | 2 | if (is_null($query = $request->get('query'))) |
|
140 | return redirect()->route('log-viewer::logs.show', [$date]); |
||
|
|||
141 | |||
142 | 2 | $levels = $this->logViewer->levelsNames(); |
|
143 | 2 | $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) { |
|
144 | 2 | return Str::contains($value->header, $query); |
|
145 | 2 | })->paginate($this->perPage); |
|
146 | |||
147 | 2 | 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 | 2 | public function download($date) |
|
161 | |||
162 | /** |
||
163 | * Delete a log. |
||
164 | * |
||
165 | * @param \Illuminate\Http\Request $request |
||
166 | * |
||
167 | * @return \Illuminate\Http\JsonResponse |
||
168 | */ |
||
169 | 6 | public function delete(Request $request) |
|
170 | { |
||
171 | 6 | if ( ! $request->ajax()) |
|
172 | 2 | abort(405, 'Method Not Allowed'); |
|
173 | |||
174 | 4 | $date = $request->get('date'); |
|
175 | |||
176 | 4 | return response()->json([ |
|
177 | 4 | 'result' => $this->logViewer->delete($date) ? 'success' : 'error' |
|
178 | ]); |
||
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 | 10 | protected function view($view, $data = [], $mergeData = []) |
|
199 | |||
200 | /** |
||
201 | * Paginate logs. |
||
202 | * |
||
203 | * @param array $data |
||
204 | * @param \Illuminate\Http\Request $request |
||
205 | * |
||
206 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
207 | */ |
||
208 | 2 | protected function paginate(array $data, Request $request) |
|
209 | { |
||
210 | 2 | $data = collect($data); |
|
211 | 2 | $page = $request->get('page', 1); |
|
212 | 2 | $url = $request->url(); |
|
213 | |||
214 | 2 | return new LengthAwarePaginator( |
|
215 | 2 | $data->forPage($page, $this->perPage), |
|
216 | 2 | $data->count(), |
|
217 | 2 | $this->perPage, |
|
218 | 2 | $page, |
|
219 | 2 | compact('url') |
|
220 | ); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Get a log or fail |
||
225 | * |
||
226 | * @param string $date |
||
227 | * |
||
228 | * @return \Arcanedev\LogViewer\Entities\Log|null |
||
229 | */ |
||
230 | 10 | protected function getLogOrFail($date) |
|
243 | |||
244 | /** |
||
245 | * Prepare chart data. |
||
246 | * |
||
247 | * @param \Arcanedev\LogViewer\Tables\StatsTable $stats |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | 2 | protected function prepareChartData(StatsTable $stats) |
|
266 | |||
267 | /** |
||
268 | * Calculate the percentage. |
||
269 | * |
||
270 | * @param array $total |
||
271 | * @param array $names |
||
272 | * |
||
273 | * @return array |
||
274 | */ |
||
275 | 2 | protected function calcPercentages(array $total, array $names) |
|
290 | } |
||
291 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.