Completed
Pull Request — master (#79)
by ARCANEDEV
09:43
created

LogParser::populateEntries()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 12
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
1
<?php namespace Arcanedev\LogViewer\Helpers;
2
3
use Arcanedev\LogViewer\Utilities\LogLevels;
4
5
/**
6
 * Class     LogParser
7
 *
8
 * @package  Arcanedev\LogViewer\Helpers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class LogParser
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Parsed data.
19
     *
20
     * @var array
21
     */
22
    protected static $parsed = [];
23
24
    /* ------------------------------------------------------------------------------------------------
25
     |  Main Functions
26
     | ------------------------------------------------------------------------------------------------
27
     */
28
    /**
29
     * Parse file content.
30
     *
31
     * @param  string  $raw
32
     *
33
     * @return array
34
     */
35 576
    public static function parse($raw)
36
    {
37 576
        self::$parsed          = [];
38 576
        list($headings, $data) = self::parseRawData($raw);
39
40
        // @codeCoverageIgnoreStart
41
        if ( ! is_array($headings)) {
42
            return self::$parsed;
43
        }
44
        // @codeCoverageIgnoreEnd
45
46 576
        foreach ($headings as $heading) {
47 576
            for ($i = 0, $j = count($heading); $i < $j; $i++) {
48 576
                self::populateEntries($heading, $data, $i);
49 288
            }
50 288
        };
51
52 576
        unset($headings, $data);
53
54 576
        return array_reverse(self::$parsed);
55
    }
56
57
    /* ------------------------------------------------------------------------------------------------
58
     |  Other Functions
59
     | ------------------------------------------------------------------------------------------------
60
     */
61
    /**
62
     * Parse raw data.
63
     *
64
     * @param  string  $raw
65
     *
66
     * @return array
67
     */
68 576
    private static function parseRawData($raw)
69
    {
70 576
        $pattern = '/\['.REGEX_DATE_PATTERN.' '.REGEX_TIME_PATTERN.'\].*/';
71 576
        preg_match_all($pattern, $raw, $headings);
72 576
        $data    = preg_split($pattern, $raw);
73
74 576
        if ($data[0] < 1) {
75 576
            $trash = array_shift($data);
76 576
            unset($trash);
77 288
        }
78
79 576
        return [$headings, $data];
80
    }
81
82
    /**
83
     * Populate entries.
84
     *
85
     * @param  array  $heading
86
     * @param  array  $data
87
     * @param  int    $key
88
     */
89 576
    private static function populateEntries($heading, $data, $key)
90
    {
91 576
        foreach (LogLevels::all() as $level) {
92 576
            if (self::hasLogLevel($heading[$key], $level)) {
93 576
                self::$parsed[] = [
94 576
                    'level'  => $level,
95 576
                    'header' => $heading[$key],
96 576
                    'stack'  => $data[$key]
97 288
                ];
98 288
            }
99 288
        }
100 576
    }
101
102
    /**
103
     * Check if header has a log level.
104
     *
105
     * @param  string  $heading
106
     * @param  string  $level
107
     *
108
     * @return bool
109
     */
110 576
    private static function hasLogLevel($heading, $level)
111
    {
112 576
        return str_contains(strtolower($heading), strtolower('.' . $level));
113
    }
114
}
115