Completed
Push — develop ( 7779bb...64bf16 )
by Nate
03:49
created

LogViewerTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 102
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
logFile() 0 1 ?
A lineRegex() 0 4 1
A lineMatcher() 0 13 1
A lineExtra() 0 6 1
A getLogItems() 0 25 2
A parseFile() 0 22 5
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember
7
 */
8
9
namespace flipbox\ember\controllers;
10
11
use Craft;
12
use craft\helpers\DateTimeHelper;
13
use yii\data\ArrayDataProvider;
14
use yii\data\DataProviderInterface;
15
use yii\data\Pagination;
16
17
/**
18
 * @author Flipbox Factory <[email protected]>
19
 * @since 1.0.8
20
 */
21
trait LogViewerTrait
22
{
23
    /**
24
     * @return string
25
     */
26
    protected abstract function logFile(): string;
27
28
    /**
29
     * @return string
30
     */
31
    protected function lineRegex(): string
32
    {
33
        return '/^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) \[(.*)\]\[(.*)\]\[(.*)\]\[(.*)\]\[(.*)\] (.*)/';
34
    }
35
36
    /**
37
     * @param array $matches
38
     * @param array $log
39
     * @return array
40
     */
41
    protected function lineMatcher(array $matches, array $log): array
0 ignored issues
show
Unused Code introduced by
The parameter $log is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        return [
44
            'time' => DateTimeHelper::toDateTime($matches[1]),
45
            'message' => $matches[7],
46
            'ip' => $matches[2],
47
            'userId' => $matches[3],
48
            'sessionId' => $matches[4],
49
            'level' => $matches[5],
50
            'category' => $matches[6],
51
            'vars' => ''
52
        ];
53
    }
54
55
    /**
56
     * @param string $line
57
     * @param array $log
58
     * @return array
59
     */
60
    protected function lineExtra(string $line, array $log): array
61
    {
62
        $log['vars'] .= utf8_encode($line);
63
64
        return $log;
65
    }
66
67
    /**
68
     * @return DataProviderInterface
69
     * @throws \Exception
70
     */
71
    protected function getLogItems(): DataProviderInterface
72
    {
73
        $file = Craft::getAlias($this->logFile());
74
75
        if (!is_file($file)) {
76
            throw new \Exception("'$file' is not found");
77
        }
78
79
        return new ArrayDataProvider([
80
            'allModels' => $this->parseFile($file),
81
            'sort' => [
82
                'attributes' => [
83
                    'time' => ['default' => SORT_DESC],
84
                    'level' => ['default' => SORT_DESC]
85
                ],
86
            ],
87
            'pagination' => [
88
                'class' => Pagination::class,
89
                'pageSizeParam' => 'size',
90
                'pageParam' => 'page',
91
                'pageSizeLimit' => 'limit',
92
                'defaultPageSize' => 200,
93
            ]
94
        ]);
95
    }
96
97
    /**
98
     * @param string
99
     */
100
    protected function parseFile($file)
101
    {
102
        $lines = [];
103
        $log = [];
104
        if ($file = fopen($file, "r")) {
105
106
            while (($line = fgets($file)) !== false) {
107
                // Log line
108
                if (preg_match($this->lineRegex(), $line, $matches)) {
109
                    if (!empty($log)) {
110
                        $lines[] = $log;
111
                    }
112
                    $log = $this->lineMatcher($matches, $log);
113
                } else {
114
                    $log = $this->lineExtra($line, $log);
115
                }
116
            }
117
            fclose($file);
118
119
        }
120
        return $lines;
121
    }
122
}