Completed
Pull Request — master (#2)
by ARCANEDEV
02:54
created

LogViewerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

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 7
cp 0
rs 9.6667
cc 1
eloc 5
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 Illuminate\Pagination\LengthAwarePaginator;
8
9
/**
10
 * Class     LogViewerController
11
 *
12
 * @package  Arcanesoft\Foundation\Http\Controllers
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class LogViewerController extends FoundationController
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The LogViewer instance.
23
     *
24
     * @var \Arcanedev\LogViewer\Contracts\LogViewerInterface
25
     */
26
    protected $logViewer;
27
28
    /**
29
     * Logs per page.
30
     *
31
     * @var int
32
     */
33
    protected $perPage = 30;
34
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Constructor
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    /**
40
     * LogViewerController constructor.
41
     *
42
     * @param  \Arcanedev\LogViewer\Contracts\LogViewerInterface  $logViewer
43
     */
44
    public function __construct(LogViewerInterface $logViewer)
45
    {
46
        parent::__construct();
47
48
        $this->logViewer = $logViewer;
49
50
        $this->setCurrentPage('foundation-logviewer');
51
        $this->addBreadcrumb('LogViewer', 'foundation::log-viewer.index');
52
    }
53
54
    /* ------------------------------------------------------------------------------------------------
55
     |  Main Functions
56
     | ------------------------------------------------------------------------------------------------
57
     */
58
    public function index()
59
    {
60
        $stats    = $this->logViewer->statsTable();
61
        $percents = $this->calcPercentages($stats->footer(), $stats->header());
62
63
        $this->addBreadcrumb('Dashboard');
64
65
        return $this->view('log-viewer.dashboard', compact('percents'));
66
    }
67
68
    public function listLogs()
69
    {
70
        $stats   = $this->logViewer->statsTable();
71
        $headers = $stats->header();
72
        // $footer   = $stats->footer();
73
74
        $page    = request('page', 1);
75
        $offset  = ($page * $this->perPage) - $this->perPage;
76
77
        $rows    = new LengthAwarePaginator(
78
            array_slice($stats->rows(), $offset, $this->perPage, true),
79
            count($stats->rows()),
80
            $this->perPage,
81
            $page
82
        );
83
        $rows->setPath(request()->url());
84
85
        $this->addBreadcrumb('Logs List');
86
87
        return $this->view('log-viewer.list', compact('headers', 'rows', 'footer'));
88
    }
89
90
    /**
91
     * Show the log.
92
     *
93
     * @param  string  $date
94
     *
95
     * @return \Illuminate\View\View
96
     */
97
    public function show($date)
98
    {
99
        $log     = $this->getLogOrFail($date);
100
        $levels  = $this->logViewer->levelsNames();
101
        $entries = $log->entries()->paginate($this->perPage);
102
103
        $this->addBreadcrumb('Logs List', 'foundation::log-viewer.logs.list');
104
        $this->addBreadcrumb($date);
105
106
        return $this->view('log-viewer.show', compact('log', 'levels', 'entries'));
107
    }
108
109
    /* ------------------------------------------------------------------------------------------------
110
     |  Other Functions
111
     | ------------------------------------------------------------------------------------------------
112
     */
113
    /**
114
     * Get a log or fail
115
     *
116
     * @param  string  $date
117
     *
118
     * @return Log|null
119
     */
120
    private function getLogOrFail($date)
121
    {
122
        $log = null;
123
124
        try {
125
            $log = $this->logViewer->get($date);
126
        }
127
        catch(LogNotFound $e) {
128
            abort(404, $e->getMessage());
129
        }
130
131
        return $log;
132
    }
133
134
    /**
135
     * Calculate the percentage
136
     *
137
     * @param  array  $total
138
     * @param  array  $names
139
     *
140
     * @return array
141
     */
142
    private function calcPercentages(array $total, array $names)
143
    {
144
        $percents = [];
145
        $all      = array_get($total, 'all');
146
147
        foreach ($total as $level => $count) {
148
            $percents[$level] = [
149
                'name'    => $names[$level],
150
                'count'   => $count,
151
                'percent' => round(($count / $all) * 100, 2),
152
            ];
153
        }
154
155
        return $percents;
156
    }
157
}
158