Completed
Push — id3-metadata-objects ( 491068...1cf97b )
by Daniel
09:08
created

PictureFrameReader   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 193
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 20
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 193
ccs 0
cts 59
cp 0
rs 10

11 Methods

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