Completed
Push — master ( 45c9ef...2c93f5 )
by ARCANEDEV
06:59
created

LogViewerController::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Arcanedev\LogViewer\Http\Controllers;
2
3
use Arcanedev\LogViewer\Bases\Controller;
4
use Arcanedev\LogViewer\Exceptions\LogNotFoundException;
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 120
     |  Constructor
29
     | ------------------------------------------------------------------------------------------------
30 120
     */
31
    /**
32 120
     * LogViewerController constructor.
33 120
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
38
        $this->perPage = config('log-viewer.per-page', $this->perPage);
39
    }
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Main Functions
43
     | ------------------------------------------------------------------------------------------------
44 12
     */
45
    /**
46 12
     * Show the dashboard.
47 12
     *
48 12
     * @return \Illuminate\View\View
49
     */
50 12
    public function index()
51
    {
52
        $stats    = $this->logViewer->statsTable();
53 12
        $reports  = $stats->totalsJson();
54
        $percents = $this->calcPercentages($stats->footer(), $stats->header());
55 12
56
        return $this->view('dashboard', compact('reports', 'percents'));
57 12
    }
58
59
    /**
60 12
     * List all logs.
61 12
     *
62
     * @param  \Illuminate\Http\Request  $request
63 12
     *
64 12
     * @return \Illuminate\View\View
65 12
     */
66 12
    public function listLogs(Request $request)
67
    {
68 9
        $stats   = $this->logViewer->statsTable();
69
        $headers = $stats->header();
70 12
        $rows    = $this->paginate($stats->rows(), $request);
71
72 12
        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 24
    public function show($date)
83
    {
84 24
        $log     = $this->getLogOrFail($date);
85 12
        $levels  = $this->logViewer->levelsNames();
86 12
        $entries = $log->entries()->paginate($this->perPage);
87
88 12
        return $this->view('show', compact('log', 'levels', 'entries'));
89 6
    }
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 24
    public function showByLevel($date, $level)
100
    {
101 24
        $log = $this->getLogOrFail($date);
102
103 24
        if ($level === 'all')
104 12
            return redirect()->route($this->showRoute, [$date]);
105
106
        $levels  = $this->logViewer->levelsNames();
107 12
        $entries = $this->logViewer
108 12
            ->entries($date, $level)
109 12
            ->paginate($this->perPage);
110 12
111
        return $this->view('show', compact('log', 'levels', 'entries'));
112 12
    }
113
114
    /**
115
     * Download the log
116
     *
117
     * @param  string  $date
118
     *
119
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
120
     */
121
    public function download($date)
122 12
    {
123
        return $this->logViewer->download($date);
124 12
    }
125
126
    /**
127
     * Delete a log.
128
     *
129
     * @param  \Illuminate\Http\Request  $request
130
     *
131
     * @return \Illuminate\Http\JsonResponse
132 36
     */
133
    public function delete(Request $request)
134 36
    {
135
        if ( ! $request->ajax())
136 24
            abort(405, 'Method Not Allowed');
137
138 24
        $date = $request->get('date');
139 9
140
        return response()->json([
141 12
            'result' => $this->logViewer->delete($date) ? 'success' : 'error'
142
        ]);
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 48
     * @return \Illuminate\Pagination\LengthAwarePaginator
156
     */
157 48
    protected function paginate(array $data, Request $request)
158
    {
159
        $page   = $request->get('page', 1);
160 48
        $offset = ($page * $this->perPage) - $this->perPage;
161
        $items  = array_slice($data, $offset, $this->perPage, true);
162 39
        $rows   = new LengthAwarePaginator($items, count($data), $this->perPage, $page);
163 12
164
        $rows->setPath($request->url());
165
166 36
        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
    protected function getLogOrFail($date)
177 12
    {
178
        $log = null;
179 12
180 12
        try {
181
            $log = $this->logViewer->get($date);
182 12
        }
183 12
        catch (LogNotFoundException $e) {
184 12
            abort(404, $e->getMessage());
185 12
        }
186 12
187
        return $log;
188 9
    }
189
190 12
    /**
191
     * Calculate the percentage.
192
     *
193
     * @param  array  $total
194
     * @param  array  $names
195
     *
196
     * @return array
197
     */
198
    protected function calcPercentages(array $total, array $names)
199
    {
200
        $percents = [];
201
        $all      = Arr::get($total, 'all');
202
203
        foreach ($total as $level => $count) {
204
            $percents[$level] = [
205
                'name'    => $names[$level],
206
                'count'   => $count,
207
                'percent' => $all ? round(($count / $all) * 100, 2) : 0,
208
            ];
209
        }
210
211
        return $percents;
212
    }
213
}
214