Passed
Push — master ( 368ea8...00a7e0 )
by Kris
02:37 queued 01:05
created

SoftwareLogParser   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
eloc 32
c 1
b 0
f 0
dl 0
loc 218
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumns() 0 9 3
A __construct() 0 3 1
A addFile() 0 3 1
A getKnownFormats() 0 3 1
A getPaths() 0 3 1
A addColumn() 0 19 4
A addPath() 0 3 1
A getSoftware() 0 3 1
A getPrettyName() 0 3 1
A addFormat() 0 3 1
A getFiles() 0 3 1
1
<?php declare(strict_types=1);
2
3
/** 
4
 *  ___                      _
5
 * | _ \ __ _  _ _  ___ ___ | | ___  __ _
6
 * |  _// _` || '_|(_-</ -_)| |/ _ \/ _` |
7
 * |_|  \__,_||_|  /__/\___||_|\___/\__, |
8
 *                                  |___/
9
 * 
10
 * (c) Kristuff <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * @version    0.2.0
16
 * @copyright  2017-2020 Kristuff
17
 */
18
19
namespace Kristuff\Parselog\Software;
20
21
use Kristuff\Parselog\Core\LogEntryFactoryInterface;
22
use Kristuff\Parselog\LogParser;
23
24
/**
25
 * Abstrat base class for software parser
26
 */
27
abstract class SoftwareLogParser extends LogParser
28
{
29
    /** 
30
     * The default log format 
31
     * 
32
     * @access protected
33
     * @var string
34
     */
35
    protected $defaultFormat = '';
36
37
    /** 
38
     * The software name
39
     * 
40
     * @access protected
41
     * @var string 
42
     */
43
    protected $software = '';
44
45
    /** 
46
     * The log pretty name
47
     * 
48
     * @access protected
49
     * @var string 
50
     */
51
    protected $prettyName = '';
52
53
    /** 
54
     * @var array 
55
     */
56
    protected $knownFormats = [];
57
58
    /** 
59
     * @var array 
60
     */
61
    protected $columns = [];
62
63
    /** 
64
     * The log file names
65
     * 
66
     * @access protected
67
     * @var array 
68
     */
69
    protected $files = [];
70
71
    /** 
72
     * The log file paths
73
     * 
74
     * @access protected
75
     * @var array 
76
     */
77
    protected $paths = [];
78
79
    /**
80
     * Constructor
81
     * 
82
     * @access public
83
     * @param string                    $format    
84
     * @param LogEntryFactoryInterface  $factory        
85
     * 
86
     * @return void
87
     */
88
    public function __construct(string $format = null, LogEntryFactoryInterface $factory = null)
89
    {
90
        parent::__construct($format ?? $this->defaultFormat, $factory);
91
    }
92
93
    /**
94
     * Gets the pretty name 
95
     * 
96
     * @access public
97
     * 
98
     * @return string
99
     */
100
    public function getPrettyName(): string
101
    {
102
        return $this->prettyName;
103
    }
104
105
    /**
106
     * Gets the software name 
107
     * 
108
     * @access public
109
     * 
110
     * @return string
111
     */
112
    public function getSoftware(): string
113
    {
114
        return $this->software;
115
    }
116
    
117
    /**
118
     * Add a format to the known formats list
119
     * 
120
     * @access protected
121
     * @param string    $name        Common name
122
     * @param string    $format      The log format
123
     * 
124
     * @return void
125
     */
126
    protected function addFormat(string $name, string $format): void
127
    {
128
        $this->knownFormats[$name] = $format;
129
    }
130
131
    /**
132
     * Gets the list of known log formats
133
     * 
134
     * @access public
135
     * 
136
     * @return array                An indexed array name/format
137
     */
138
    public function getKnownFormats(): array
139
    {
140
        return $this->knownFormats;
141
    }
142
143
    /**
144
     * Add a file to the known files list
145
     * 
146
     * @access protected
147
     * @param string    $fielName    The log file name
148
     * 
149
     * @return void
150
     */
151
    protected function addFile(string $fileName): void
152
    {
153
        $this->files[] = $fileName;
154
    }
155
156
    /**
157
     * Gets the list of known files names for current parser
158
     *  
159
     * 
160
     * @access public
161
     * 
162
     * @return array
163
     */
164
    public function getFiles(): array
165
    {
166
        return $this->files;
167
    }
168
169
    /**
170
     * Add a path to the known paths list
171
     * 
172
     * @access protected
173
     * @param string    $path        The log path
174
     * 
175
     * @return void
176
     */
177
    protected function addPath(string $path): void
178
    {
179
        $this->paths[] = $path;
180
    }
181
182
    /**
183
     * Gets the list of known paths for current parser
184
     * 
185
     * @access public
186
     * 
187
     * @return array
188
     */
189
    public function getPaths(): array
190
    {
191
        return $this->paths;
192
    }
193
194
    /**
195
     * Add a column definition to the list
196
     * 
197
     * @access protected
198
     * @param string    $placeholder        The column placeholder
199
     * @param string    $propertyName       The column name
200
     * @param string    $prettyName         The column pretty name
201
     * @param string    $pattern            The column regex expression
202
     * @param bool      $required           False if the entire column can be missing in output (default is true)
203
     * 
204
     * @return void
205
     */
206
    protected function addColumn(string $placeholder, string $propertyName, string $prettyName, string $pattern, bool $required = true): void
207
    {
208
        $this->columns[] = [
209
            'placeholder'   => $placeholder,
210
            'propertyName'  => $propertyName,
211
            'prettyName'    => $prettyName,
212
            'pattern'       => $pattern,
213
            'required'      => $required,
214
        ];
215
216
        if (!empty($pattern)){
217
218
            // optional columns ?
219
            // adjust pattern for nullable columns and add space after placeholder
220
            // - $format = '%t %l %P %E: %a %M';
221
            // + $format = '%t %l (%P )?(%E: )?(%a )?%M';
222
            $this->addPattern( 
223
                $required ? $placeholder : $placeholder . ' ' , 
224
                $required ? $pattern     : '(' . $pattern . ' )?' 
225
            );
226
        }
227
    }
228
229
    /**
230
     * Gets the list of columns according to current log format
231
     * 
232
     * @access public
233
     * 
234
     * @return array
235
     */
236
    public function getColumns()
237
    {
238
        $cols = [];
239
        foreach ($this->columns as $column){
240
            if (strpos($this->getFormat(), $column['placeholder']) !== false){
241
                $cols[] = $column;
242
            }
243
        }
244
        return $cols;
245
    }
246
}