Completed
Push — id3-metadata-objects ( 33018f...0cc3db )
by Daniel
07:46
created

PictureFrameReader   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 20
c 2
b 0
f 2
lcom 1
cbo 2
dl 0
loc 187
ccs 0
cts 60
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A readEncoding() 0 6 1
A getEncoding() 0 8 2
A readMimeType() 0 16 3
A getMimeType() 0 8 2
A readType() 0 8 1
A getType() 0 8 2
A readDescription() 0 18 3
A getDescription() 0 8 2
A readData() 0 12 2
A getData() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Metadata project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2\Reader;
9
10
use GravityMedia\Metadata\ID3v2\StreamContainer;
11
12
/**
13
 * ID3v2 picture frame reader class.
14
 *
15
 * @package GravityMedia\Metadata\ID3v2\Reader
16
 */
17
class PictureFrameReader extends StreamContainer
18
{
19
    /**
20
     * @var int
21
     */
22
    private $encoding;
23
24
    /**
25
     * @var string
26
     */
27
    private $mimeType;
28
29
    /**
30
     * @var int
31
     */
32
    private $type;
33
34
    /**
35
     * @var string
36
     */
37
    private $description;
38
39
    /**
40
     * @var string
41
     */
42
    private $data;
43
44
    /**
45
     * Read encoding.
46
     *
47
     * @return int
48
     */
49
    protected function readEncoding()
50
    {
51
        $this->getStream()->seek($this->getOffset());
52
53
        return $this->getStream()->readUInt8();
54
    }
55
56
    /**
57
     * Get encoding.
58
     *
59
     * @return int
60
     */
61
    public function getEncoding()
62
    {
63
        if (null === $this->encoding) {
64
            $this->encoding = $this->readEncoding();
65
        }
66
67
        return $this->encoding;
68
    }
69
70
    /**
71
     * Read mime type.
72
     *
73
     * @return string
74
     */
75
    protected function readMimeType()
76
    {
77
        $this->getStream()->seek($this->getOffset() + 1);
78
79
        $mimeType = '';
80
        while (!$this->getStream()->eof()) {
81
            $char = $this->getStream()->read(1);
82
            if ("\x00" === $char) {
83
                break;
84
            }
85
86
            $mimeType .= $char;
87
        }
88
89
        return $mimeType;
90
    }
91
92
    /**
93
     * Get mime type.
94
     *
95
     * @return string
96
     */
97
    public function getMimeType()
98
    {
99
        if (null === $this->mimeType) {
100
            $this->mimeType = $this->readMimeType();
101
        }
102
103
        return $this->mimeType;
104
    }
105
106
    /**
107
     * Get type.
108
     *
109
     * @return int
110
     */
111
    protected function readType()
112
    {
113
        $offset = strlen($this->getMimeType()) + 2;
114
115
        $this->getStream()->seek($this->getOffset() + $offset);
116
117
        return $this->getStream()->readUInt8();
118
    }
119
120
    /**
121
     * Get type.
122
     *
123
     * @return int
124
     */
125
    public function getType()
126
    {
127
        if (null === $this->type) {
128
            $this->type = $this->readType();
129
        }
130
131
        return $this->type;
132
    }
133
134
    /**
135
     * Read description.
136
     *
137
     * @return string
138
     */
139
    protected function readDescription()
140
    {
141
        $offset = strlen($this->getMimeType()) + 3;
142
143
        $this->getStream()->seek($this->getOffset() + $offset);
144
145
        $description = '';
146
        while (!$this->getStream()->eof()) {
147
            $char = $this->getStream()->read(1);
148
            if ("\x00" === $char) {
149
                break;
150
            }
151
152
            $description .= $char;
153
        }
154
155
        return $description;
156
    }
157
158
    /**
159
     * Get description.
160
     *
161
     * @return string
162
     */
163
    public function getDescription()
164
    {
165
        if (null === $this->description) {
166
            $this->description = $this->readDescription();
167
        }
168
169
        return $this->description;
170
    }
171
172
    /**
173
     * Read data.
174
     *
175
     * @return string
176
     */
177
    protected function readData()
178
    {
179
        $offset = strlen($this->getMimeType()) + strlen($this->getDescription()) + 4;
180
        $length = $this->getStream()->getSize() - $offset;
181
        if ($length < 1) {
182
            return '';
183
        }
184
185
        $this->getStream()->seek($this->getOffset() + $offset);
186
187
        return $this->getStream()->read($length);
188
    }
189
190
    /**
191
     * Get data.
192
     *
193
     * @return string
194
     */
195
    public function getData()
196
    {
197
        if (null === $this->data) {
198
            $this->data = $this->readData();
199
        }
200
201
        return $this->data;
202
    }
203
}
204