Completed
Push — master ( 3a2d10...8dcf54 )
by ARCANEDEV
04:59
created

LogViewerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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