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

LanguageTextFrameReader::readLanguage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
crap 12
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 language text frame reader class.
14
 *
15
 * @package GravityMedia\Metadata\ID3v2\Reader
16
 */
17
class LanguageTextFrameReader extends StreamContainer
18
{
19
    /**
20
     * @var int
21
     */
22
    private $encoding;
23
24
    /**
25
     * @var string
26
     */
27
    private $language;
28
29
    /**
30
     * @var string
31
     */
32
    private $text;
33
34
    /**
35
     * Read encoding.
36
     *
37
     * @return int
38
     */
39
    protected function readEncoding()
40
    {
41
        $this->getStream()->seek($this->getOffset());
42
43
        return $this->getStream()->readUInt8();
44
    }
45
46
    /**
47
     * Get encoding.
48
     *
49
     * @return int
50
     */
51
    public function getEncoding()
52
    {
53
        if (null === $this->encoding) {
54
            $this->encoding = $this->readEncoding();
55
        }
56
57
        return $this->encoding;
58
    }
59
60
    /**
61
     * Read language.
62
     *
63
     * @return string
64
     */
65
    public function readLanguage()
66
    {
67
        $this->getStream()->seek($this->getOffset() + 1);
68
        $language = strtoupper(trim($this->getStream()->read(3), "\x00"));
69
70
        if ('XXX' === $language || strlen($language) !== 3) {
71
            $language = 'UND';
72
        }
73
74
        return $language;
75
    }
76
77
    /**
78
     * Get language.
79
     *
80
     * @return string
81
     */
82
    public function getLanguage()
83
    {
84
        if (null === $this->language) {
85
            $this->language = $this->readLanguage();
86
        }
87
88
        return $this->language;
89
    }
90
91
    /**
92
     * Read text.
93
     *
94
     * @return string
95
     */
96 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...
97
    {
98
        $offset = 4;
99
        $length = $this->getStream()->getSize() - $offset;
100
        if ($length < 1) {
101
            return '';
102
        }
103
104
        $this->getStream()->seek($this->getOffset() + $offset);
105
106
        return $this->getStream()->read($length);
107
    }
108
109
    /**
110
     * Get text.
111
     *
112
     * @return string
113
     */
114
    public function getText()
115
    {
116
        if (null === $this->text) {
117
            $this->text = $this->readText();
118
        }
119
120
        return $this->text;
121
    }
122
}
123