Completed
Push — id3-metadata-objects ( e37014...ee91c4 )
by Daniel
12:20
created

FrameReader::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

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 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
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 reader class.
15
 *
16
 * @package GravityMedia\Metadata\ID3v2\Metadata
17
 */
18
class FrameReader 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
     * Set ID3v2 frame name.
62
     *
63
     * @param string $name
64
     *
65
     * @return $this
66
     */
67
    public function setName($name)
68
    {
69
        $this->name = $name;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Read ID3v2 frame size.
76
     *
77
     * @return int
78
     */
79
    protected function readSize()
80
    {
81 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...
82
            $this->getStream()->seek($this->getOffset() + 3);
83
84
            return $this->getStream()->readUInt24();
85
        }
86
87
        $this->getStream()->seek($this->getOffset() + 4);
88
89
        $value = $this->getStream()->readUInt32();
90
91
        if (Version::VERSION_23 === $this->getVersion()) {
92
            return $value;
93
        }
94
95
        return $this->getSynchsafeIntegerFilter()->decode($value);
96
    }
97
98
    /**
99
     * Read ID3v2 frame flags.
100
     *
101
     * @return bool[]
102
     */
103
    protected function readFlags()
104
    {
105
        if (Version::VERSION_22 === $this->getVersion()) {
106
            return [];
107
        }
108
109
        $this->getStream()->seek($this->getOffset() + 8);
110
111
        $flags = $this->getStream()->readUInt16();
112
113
        if (Version::VERSION_23 === $this->getVersion()) {
114
            return [
115
                FrameFlag::FLAG_TAG_ALTER_PRESERVATION => (bool)($flags & 0x8000),
116
                FrameFlag::FLAG_FILE_ALTER_PRESERVATION => (bool)($flags & 0x4000),
117
                FrameFlag::FLAG_READ_ONLY => (bool)($flags & 0x2000),
118
                FrameFlag::FLAG_COMPRESSION => (bool)($flags & 0x0080),
119
                FrameFlag::FLAG_ENCRYPTION => (bool)($flags & 0x0040),
120
                FrameFlag::FLAG_GROUPING_IDENTITY => (bool)($flags & 0x0020),
121
            ];
122
        }
123
124
        return [
125
            FrameFlag::FLAG_TAG_ALTER_PRESERVATION => (bool)($flags & 0x4000),
126
            FrameFlag::FLAG_FILE_ALTER_PRESERVATION => (bool)($flags & 0x2000),
127
            FrameFlag::FLAG_READ_ONLY => (bool)($flags & 0x1000),
128
            FrameFlag::FLAG_GROUPING_IDENTITY => (bool)($flags & 0x0040),
129
            FrameFlag::FLAG_COMPRESSION => (bool)($flags & 0x0008),
130
            FrameFlag::FLAG_ENCRYPTION => (bool)($flags & 0x0004),
131
            FrameFlag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x0002),
132
            FrameFlag::FLAG_DATA_LENGT_INDICATOR => (bool)($flags & 0x0001),
133
        ];
134
    }
135
136
    /**
137
     * Read ID3v2 frame data length.
138
     *
139
     * @return int
140
     */
141
    protected function readDataLength()
142
    {
143
        if (!$this->isFlagEnabled(FrameFlag::FLAG_DATA_LENGT_INDICATOR)) {
144
            return $this->getSize();
145
        }
146
147
        $this->getStream()->seek($this->getOffset() + 10);
148
149
        return $this->getSynchsafeIntegerFilter()->decode($this->getStream()->readUInt32());
150
    }
151
152
    /**
153
     * Get ID3v2 frame data length.
154
     *
155
     * @return int
156
     */
157
    public function getDataLength()
158
    {
159
        if (null === $this->dataLength) {
160
            $this->dataLength = $this->readDataLength();
161
        }
162
163
        return $this->dataLength;
164
    }
165
166
    /**
167
     * Write ID3v2 frame data data.
168
     *
169
     * @return $this
170
     */
171
    public function write()
172
    {
173
        $this->getStream()->seek($this->getOffset());
174
175
        $this->getStream()->write($this->name);
176
177
        return $this;
178
    }
179
}
180