Completed
Push — master ( 71c1d9...f78dbb )
by ARCANEDEV
03:55
created

LogViewerController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 241
ccs 0
cts 105
cp 0
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 7

9 Methods

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