Completed
Push — id3-metadata-objects ( 501882...491068 )
by Daniel
02:42
created

FrameHeaderReader::readDataLength()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * This file is part of the Metadata package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2\Reader;
9
10
use GravityMedia\Metadata\ID3v2\Flag\FrameFlag;
11
use GravityMedia\Metadata\ID3v2\Version;
12
13
/**
14
 * ID3v2 frame header reader class.
15
 *
16
 * @package GravityMedia\Metadata\ID3v2\Reader
17
 */
18
class FrameHeaderReader extends AbstractReader
19
{
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @var int
27
     */
28
    private $dataLength;
29
30
    /**
31
     * Read ID3v2 frame name.
32
     *
33
     * @return string
34
     */
35
    protected function readName()
36
    {
37
        $this->getStream()->seek($this->getOffset());
38
39
        if (Version::VERSION_22 === $this->getVersion()) {
40
            return rtrim($this->getStream()->read(3));
41
        }
42
43
        return rtrim($this->getStream()->read(4));
44
    }
45
46
    /**
47
     * Get ID3v2 frame name.
48
     *
49
     * @return string
50
     */
51
    public function getName()
52
    {
53
        if (null === $this->name) {
54
            $this->name = $this->readName();
55
        }
56
57
        return $this->name;
58
    }
59
60
    /**
61
     * Read ID3v2 frame size.
62
     *
63
     * @return int
64
     */
65
    protected function readSize()
66
    {
67 View Code Duplication
        if (Version::VERSION_22 === $this->getVersion()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            $this->getStream()->seek($this->getOffset() + 3);
69
70
            return $this->getStream()->readUInt24();
71
        }
72
73
        $this->getStream()->seek($this->getOffset() + 4);
74
75
        $value = $this->getStream()->readUInt32();
76
77
        if (Version::VERSION_23 === $this->getVersion()) {
78
            return $value;
79
        }
80
81
        return $this->getSynchsafeIntegerFilter()->decode($value);
82
    }
83
84
    /**
85
     * Read ID3v2 frame flags.
86
     *
87
     * @return bool[]
88
     */
89
    protected function readFlags()
90
    {
91
        if (Version::VERSION_22 === $this->getVersion()) {
92
            return [];
93
        }
94
95
        $this->getStream()->seek($this->getOffset() + 8);
96
97
        $flags = $this->getStream()->readUInt16();
98
99
        if (Version::VERSION_23 === $this->getVersion()) {
100
            return [
101
                FrameFlag::FLAG_TAG_ALTER_PRESERVATION => (bool)($flags & 0x8000),
102
                FrameFlag::FLAG_FILE_ALTER_PRESERVATION => (bool)($flags & 0x4000),
103
                FrameFlag::FLAG_READ_ONLY => (bool)($flags & 0x2000),
104
                FrameFlag::FLAG_COMPRESSION => (bool)($flags & 0x0080),
105
                FrameFlag::FLAG_ENCRYPTION => (bool)($flags & 0x0040),
106
                FrameFlag::FLAG_GROUPING_IDENTITY => (bool)($flags & 0x0020),
107
            ];
108
        }
109
110
        return [
111
            FrameFlag::FLAG_TAG_ALTER_PRESERVATION => (bool)($flags & 0x4000),
112
            FrameFlag::FLAG_FILE_ALTER_PRESERVATION => (bool)($flags & 0x2000),
113
            FrameFlag::FLAG_READ_ONLY => (bool)($flags & 0x1000),
114
            FrameFlag::FLAG_GROUPING_IDENTITY => (bool)($flags & 0x0040),
115
            FrameFlag::FLAG_COMPRESSION => (bool)($flags & 0x0008),
116
            FrameFlag::FLAG_ENCRYPTION => (bool)($flags & 0x0004),
117
            FrameFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x0002),
118
            FrameFlag::FLAG_DATA_LENGT_INDICATOR => (bool)($flags & 0x0001),
119
        ];
120
    }
121
122
    /**
123
     * Read ID3v2 frame data length.
124
     *
125
     * @return int
126
     */
127
    protected function readDataLength()
128
    {
129
        if (!$this->isFlagEnabled(FrameFlag::FLAG_DATA_LENGT_INDICATOR)) {
130
            return $this->getSize();
131
        }
132
133
        $this->getStream()->seek($this->getOffset() + 10);
134
135
        return $this->getSynchsafeIntegerFilter()->decode($this->getStream()->readUInt32());
136
    }
137
138
    /**
139
     * Get ID3v2 frame data length.
140
     *
141
     * @return int
142
     */
143
    public function getDataLength()
144
    {
145
        if (null === $this->dataLength) {
146
            $this->dataLength = $this->readDataLength();
147
        }
148
149
        return $this->dataLength;
150
    }
151
}
152