Completed
Push — master ( 29c7fd...0622b2 )
by Dev
53:37 queued 52:34
created

LogParser::parse()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
namespace PiedWeb\LogsAnalyzer;
4
5
class LogParser extends \Kassner\LogParser\LogParser
6
{
7
    protected $domain;
8
    protected $type;
9
10
    public function setDomain(string $domain)
11
    {
12
        $this->domain = $domain;
13
    }
14
15
    public function setType(string $type)
16
    {
17
        $this->type = $type;
18
    }
19
20
    public function parse($line)
21
    {
22
        if (!preg_match($this->pcreFormat, $line, $matches)) {
23
            throw new \Kassner\LogParser\FormatException($line);
24
        }
25
26
        $entry = $this->createEntry();
27
        foreach (array_filter(array_keys($matches), 'is_string') as $key) {
28
            $setter = 'set'.$key;
29
            if (method_exists($entry, $setter)) {
30
                $entry->$setter($matches[$key]);
31
            }
32
        }
33
34
        return $entry;
35
    }
36
37
    /**
38
     * @return LogLine
39
     */
40
    protected function createEntry()
41
    {
42
        return null === $this->type ? new LogLine($this->domain) : new $this->type($this->domain);
43
    }
44
}
45