Completed
Push — min-php71 ( f5a25a )
by James
05:53
created

AbstractReader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 52
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test() 0 6 1
A read() 0 10 2
A match() 0 10 2
getRegex() 0 1 ?
1
<?php
2
declare(strict_types=1);
3
4
namespace BrowscapPHP\Util\Logfile;
5
6
use BrowscapPHP\Exception\ReaderException;
7
8
/**
9
 * abstract parent class for all readers
10
 */
11
abstract class AbstractReader implements ReaderInterface
12
{
13
    /**
14
     * @param string $line
15
     *
16
     * @return bool
17
     */
18
    public function test(string $line) : bool
19
    {
20
        $matches = $this->match($line);
21
22
        return isset($matches['userAgentString']);
23
    }
24
25
    /**
26
     * @param string $line
27
     *
28
     * @throws \BrowscapPHP\Exception\ReaderException
29
     * @return string
30
     */
31
    public function read(string $line) : string
32
    {
33
        $matches = $this->match($line);
34
35
        if (!isset($matches['userAgentString'])) {
36
            throw ReaderException::userAgentParserError($line);
37
        }
38
39
        return $matches['userAgentString'];
40
    }
41
42
    /**
43
     * @param string $line
44
     *
45
     * @return array
46
     */
47
    protected function match(string $line) : array
48
    {
49
        $matches = [];
50
51
        if (preg_match($this->getRegex(), $line, $matches)) {
52
            return $matches;
53
        }
54
55
        return [];
56
    }
57
58
    /**
59
     * @return string
60
     */
61
    abstract protected function getRegex() : string;
62
}
63