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

AbstractReader::match()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
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