File::extractHeaderData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 11
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Crossjoin\Browscap\Source\Ini;
5
6
use Crossjoin\Browscap\Exception\ParserRuntimeException;
7
use Crossjoin\Browscap\Exception\SourceUnavailableException;
8
use Crossjoin\Browscap\Source\FileAbstract;
9
use Crossjoin\Browscap\Source\SourceInterface;
10
use Crossjoin\Browscap\Type;
11
12
/**
13
 * Class File
14
 *
15
 * @package Crossjoin\Browscap\Source\Ini
16
 * @author Christoph Ziegenberg <[email protected]>
17
 * @link https://github.com/crossjoin/browscap
18
 */
19
class File extends FileAbstract implements SourceInterface
20
{
21
    use ParseHeaderSectionTrait;
22
    use DataSetsFromContentTrait { getContent as private getContentIgnore; }
23
24
    const HEADER_PATTERN = 'GJK_Browscap_Version';
25
26
    /**
27
     * @var int
28
     */
29
    protected $releaseTime;
30
31
    /**
32
     * @var int
33
     */
34
    protected $version;
35
36
    /**
37
     * @var int
38
     */
39
    protected $type;
40
41
    /**
42
     * @inheritdoc
43
     *
44
     * @throws ParserRuntimeException
45
     * @throws SourceUnavailableException
46
     */
47
    public function getReleaseTime() : int
48
    {
49
        if ($this->releaseTime === null) {
50
            $this->extractHeaderData();
51
        }
52
53
        return $this->releaseTime;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     *
59
     * @throws ParserRuntimeException
60
     * @throws SourceUnavailableException
61
     */
62
    public function getVersion() : int
63
    {
64
        if ($this->version === null) {
65
            $this->extractHeaderData();
66
        }
67
68
        return $this->version;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     *
74
     * @throws ParserRuntimeException
75
     * @throws SourceUnavailableException
76
     */
77
    public function getType() : int
78
    {
79
        if ($this->type === null) {
80
            $this->extractHeaderData();
81
        }
82
83
        return $this->type;
84
    }
85
86
    /**
87
     * Extracts the header data from the file. This is typically the first block in the file
88
     * ofter some lines of comments, with a special section name (defined in self::HEADER_PATTERN).
89
     *
90
     * @throws ParserRuntimeException
91
     * @throws SourceUnavailableException
92
     */
93
    protected function extractHeaderData()
94
    {
95
        // Set defaults
96
        $this->version = 0;
97
        $this->type = Type::UNKNOWN;
98
        $this->releaseTime = 0;
99
100
        // Parse the beginning of the file
101
        foreach ($this->getDataSets() as $dataSet) {
102
            if ($dataSet->getPattern() === self::HEADER_PATTERN) {
103
                $result = $this->parseHeaderDataSet($dataSet);
104
                $this->version = $result['version'];
105
                $this->releaseTime = $result['released'];
106
                $this->type = $result['type'];
107
                break;
108
            }
109
        }
110
    }
111
112
    /**
113
     * Needs to be defined again, because the trait used here replaces
114
     * the method of the parent class.
115
     *
116
     * @inheritdoc
117
     */
118
    public function getContent() : \Generator
119
    {
120
        return parent::getContent();
121
    }
122
}
123