LogViewerController::download()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php namespace Arcanesoft\Foundation\Http\Controllers\Admin\System;
2
3
use Arcanedev\LaravelApiHelper\Traits\JsonResponses;
4
use Arcanedev\LogViewer\Contracts\LogViewer as LogViewerContract;
5
use Arcanedev\LogViewer\Entities\Log;
6
use Arcanedev\LogViewer\Entities\LogEntry;
7
use Arcanesoft\Foundation\Policies\LogViewerPolicy;
8
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Illuminate\Pagination\LengthAwarePaginator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Pagination\LengthAwarePaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class     LogViewerController
15
 *
16
 * @package  Arcanesoft\Foundation\Http\Controllers\Admin\System
17
 * @author   ARCANEDEV <[email protected]>
18
 */
19
class LogViewerController extends Controller
20
{
21
    /* -----------------------------------------------------------------
22
     |  Trait
23
     | -----------------------------------------------------------------
24
     */
25
26
    use JsonResponses;
27
28
    /* -----------------------------------------------------------------
29
     |  Properties
30
     | -----------------------------------------------------------------
31
     */
32
33
    /**
34
     * The LogViewer instance.
35
     *
36
     * @var \Arcanedev\LogViewer\Contracts\LogViewer
37
     */
38
    protected $logViewer;
39
40
    /**
41
     * Logs per page.
42
     *
43
     * @var int
44
     */
45
    protected $perPage = 30;
46
47
    /* -----------------------------------------------------------------
48
     |  Constructor
49
     | -----------------------------------------------------------------
50
     */
51
52
    /**
53
     * LogViewerController constructor.
54
     *
55
     * @param  \Arcanedev\LogViewer\Contracts\LogViewer  $logViewer
56
     */
57
    public function __construct(LogViewerContract $logViewer)
58
    {
59
        parent::__construct();
60
61
        $this->logViewer = $logViewer;
62
        $this->perPage   = config('arcanesoft.foundation.log-viewer.per-page', $this->perPage);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $this->perPage   = /** @scrutinizer ignore-call */ config('arcanesoft.foundation.log-viewer.per-page', $this->perPage);
Loading history...
63
        $this->setCurrentPage('foundation-system-logviewer');
64
        $this->addBreadcrumbRoute('LogViewer', 'admin::foundation.system.log-viewer.index');
65
    }
66
67
    /* ------------------------------------------------------------------------------------------------
68
     |  Main Functions
69
     | ------------------------------------------------------------------------------------------------
70
     */
71
72
    /**
73
     * Show the LogViewer Dashboard.
74
     *
75
     * @return \Illuminate\View\View
0 ignored issues
show
Bug introduced by
The type Illuminate\View\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
     */
77
    public function index()
78
    {
79
        $this->authorize(LogViewerPolicy::PERMISSION_DASHBOARD);
80
81
        $stats    = $this->logViewer->statsTable();
82
        $percents = $this->calcPercentages($stats->footer(), $stats->header());
83
84
        $this->setTitle('LogViewer Dashboard');
85
        $this->addBreadcrumb('Dashboard');
86
87
        return $this->view('admin.system.log-viewer.dashboard', compact('percents'));
88
    }
89
90
    /**
91
     * List all logs.
92
     *
93
     * @param  \Illuminate\Http\Request  $request
94
     *
95
     * @return \Illuminate\View\View
96
     */
97
    public function listLogs(Request $request)
98
    {
99
        $this->authorize(LogViewerPolicy::PERMISSION_LIST);
100
101
        $stats   = $this->logViewer->statsTable();
102
        $headers = $stats->header();
103
        // $footer   = $stats->footer();
104
105
        $page    = $request->get('page', 1);
106
        $offset  = ($page * $this->perPage) - $this->perPage;
107
        $rows    = new LengthAwarePaginator(
108
            array_slice($stats->rows(), $offset, $this->perPage, true),
109
            count($stats->rows()),
110
            $this->perPage,
111
            $page
112
        );
113
        $rows->setPath($request->url());
114
115
        $this->setTitle($title = trans('foundation::log-viewer.titles.logs-list'));
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

115
        $this->setTitle($title = /** @scrutinizer ignore-call */ trans('foundation::log-viewer.titles.logs-list'));
Loading history...
116
        $this->addBreadcrumb($title);
117
118
        return $this->view('admin.system.log-viewer.list', compact('headers', 'rows'));
119
    }
120
121
    /**
122
     * Show the log entries by date.
123
     *
124
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
125
     *
126
     * @return \Illuminate\View\View
127
     */
128
    public function show(Log $log)
129
    {
130
        $this->authorize(LogViewerPolicy::PERMISSION_SHOW);
131
132
        $levels  = $this->logViewer->levelsNames();
133
        $entries = $log->entries($level = 'all')->paginate($this->perPage);
134
        $query   = '';
135
136
        $this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

136
        $this->addBreadcrumbRoute(/** @scrutinizer ignore-call */ trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');
Loading history...
137
        $this->setTitle($title = "Log : {$log->date}");
138
        $this->addBreadcrumb($title);
139
140
        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'query', 'entries'));
141
    }
142
143
    /**
144
     * Filter the log entries by date and level.
145
     *
146
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
147
     * @param  string                             $level
148
     *
149
     * @return \Illuminate\View\View|\Illuminate\Http\RedirectResponse
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
     */
151
    public function showByLevel(Log $log, $level)
152
    {
153
        $this->authorize(LogViewerPolicy::PERMISSION_SHOW);
154
155
        if ($level == 'all')
156
            return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
            return /** @scrutinizer ignore-call */ redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);
Loading history...
157
158
        $levels  = $this->logViewer->levelsNames();
159
        $entries = $this->logViewer->entries($log->date, $level)->paginate($this->perPage);
160
        $query   = '';
161
162
        $this->addBreadcrumbRoute(trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

162
        $this->addBreadcrumbRoute(/** @scrutinizer ignore-call */ trans('foundation::log-viewer.titles.logs-list'), 'admin::foundation.system.log-viewer.logs.list');
Loading history...
163
164
        $this->setTitle($log->date.' | '.ucfirst($level));
165
        $this->addBreadcrumbRoute($log->date, 'admin::foundation.system.log-viewer.logs.show', [$log->date]);
166
        $this->addBreadcrumb(ucfirst($level));
167
168
        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'query', 'entries', 'level'));
169
    }
170
171
    /**
172
     * Show the log with the search query.
173
     *
174
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
175
     * @param  string                             $level
176
     * @param  \Illuminate\Http\Request           $request
177
     *
178
     * @return \Illuminate\View\View
179
     */
180
    public function search(Log $log, $level = 'all', Request $request)
181
    {
182
        if (is_null($query = $request->get('query')))
183
            return redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
            return /** @scrutinizer ignore-call */ redirect()->route('admin::foundation.system.log-viewer.logs.show', [$log->date]);
Loading history...
184
185
        $levels  = $this->logViewer->levelsNames();
186
        $entries = $log->entries($level)->filter(function (LogEntry $value) use ($query) {
187
            return Str::contains($value->header, $query);
188
        })->paginate($this->perPage);
189
190
        return $this->view('admin.system.log-viewer.show', compact('log', 'levels', 'level', 'query', 'entries'));
191
    }
192
193
    /**
194
     * Download the log.
195
     *
196
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
197
     *
198
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFo...tion\BinaryFileResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
199
     */
200
    public function download(Log $log)
201
    {
202
        $this->authorize(LogViewerPolicy::PERMISSION_DOWNLOAD);
203
204
        return $this->logViewer->download($log->date);
205
    }
206
207
    /**
208
     * Delete a log.
209
     *
210
     * @param  \Arcanedev\LogViewer\Entities\Log  $log
211
     *
212
     * @return \Illuminate\Http\JsonResponse
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
213
     */
214
    public function delete(Log $log)
215
    {
216
        $this->authorize(LogViewerPolicy::PERMISSION_DELETE);
217
218
        $date = $log->date;
219
220
        if ($this->logViewer->delete($date)) {
221
            $this->notifySuccess(
222
                $message = trans('foundation::log-viewer.messages.deleted.message', compact('date')),
0 ignored issues
show
Bug introduced by
The function trans was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

222
                $message = /** @scrutinizer ignore-call */ trans('foundation::log-viewer.messages.deleted.message', compact('date')),
Loading history...
223
                trans('foundation::log-viewer.messages.deleted.title')
224
            );
225
226
            return $this->jsonResponseSuccess(compact('message'));
227
        }
228
229
        return $this->jsonResponseError([
230
            'message' => "An error occurred while deleting the log [$date]"
231
        ]);
232
    }
233
234
    /* -----------------------------------------------------------------
235
     |  Other Methods
236
     | -----------------------------------------------------------------
237
     */
238
239
    /**
240
     * Calculate the percentage.
241
     *
242
     * @param  array  $total
243
     * @param  array  $names
244
     *
245
     * @return array
246
     */
247
    private function calcPercentages(array $total, array $names)
248
    {
249
        $percents = [];
250
        $all      = Arr::get($total, 'all');
251
252
        foreach ($total as $level => $count) {
253
            $percents[$level] = [
254
                'name'    => $names[$level],
255
                'count'   => $count,
256
                'percent' => round(($count / $all) * 100, 2),
257
            ];
258
        }
259
260
        return $percents;
261
    }
262
}
263