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

TextFrameReader::getText()   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 1
Metric Value
c 1
b 0
f 1
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\Filter\CharsetFilter;
11
use GravityMedia\Metadata\ID3v2\StreamContainer;
12
use GravityMedia\Stream\Stream;
13
14
/**
15
 * ID3v2 text frame reader class.
16
 *
17
 * @package GravityMedia\Metadata\ID3v2\Reader
18
 */
19
class TextFrameReader 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 $text;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function __construct(Stream $stream)
40
    {
41
        parent::__construct($stream);
42
43
        $this->charsetFilter = new CharsetFilter();
44
    }
45
46
    /**
47
     * Read encoding.
48
     *
49
     * @return int
50
     */
51
    protected function readEncoding()
52
    {
53
        $this->getStream()->seek($this->getOffset());
54
55
        return $this->getStream()->readUInt8();
56
    }
57
58
    /**
59
     * Get encoding.
60
     *
61
     * @return int
62
     */
63
    public function getEncoding()
64
    {
65
        if (null === $this->encoding) {
66
            $this->encoding = $this->readEncoding();
67
        }
68
69
        return $this->encoding;
70
    }
71
72
    /**
73
     * Read text.
74
     *
75
     * @return string[]
76
     */
77 View Code Duplication
    protected function readText()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        $this->getStream()->seek($this->getOffset() + 1);
80
        $text = $this->getStream()->read($this->getStream()->getSize() - 1);
81
82
        return explode("\x00", $this->charsetFilter->decode($text, $this->getEncoding()));
83
    }
84
85
    /**
86
     * Get text.
87
     *
88
     * @return string[]
89
     */
90
    public function getText()
91
    {
92
        if (null === $this->text) {
93
            $this->text = $this->readText();
94
        }
95
96
        return $this->text;
97
    }
98
}
99