Completed
Push — id3-metadata-objects ( e55453...161f4c )
by Daniel
11:12
created

TagReader::trimData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\ID3v1\Reader;
9
10
use GravityMedia\Metadata\ID3v1\Enum\Genre;
11
use GravityMedia\Metadata\ID3v1\Enum\Version;
12
use GravityMedia\Metadata\ID3v1\Tag;
13
use GravityMedia\Metadata\Metadata\MetadataInterface;
14
use GravityMedia\Metadata\Metadata\TagInterface;
15
use GravityMedia\Stream\Reader\Integer8Reader;
16
use GravityMedia\Stream\StreamInterface;
17
18
/**
19
 * ID3v1 tag reader
20
 *
21
 * @package GravityMedia\Metadata\ID3v1\Reader
22
 */
23
class TagReader
24
{
25
    /**
26
     * @var MetadataInterface
27
     */
28
    protected $metadata;
29
30
    /**
31
     * @var StreamInterface
32
     */
33
    protected $stream;
34
35
    /**
36
     * @var Integer8Reader
37
     */
38
    protected $integer8Reader;
39
40
    /**
41
     * Create ID3v1 tag reader.
42
     *
43
     * @param MetadataInterface $metadata
44
     * @param StreamInterface $stream
45
     */
46
    public function __construct(MetadataInterface $metadata, StreamInterface $stream)
47
    {
48
        $this->metadata = $metadata;
49
        $this->stream = $stream;
50
    }
51
52
    /**
53
     * Get 8-bit integer reader.
54
     *
55
     * @return Integer8Reader
56
     */
57 View Code Duplication
    public function getInteger8Reader()
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...
58
    {
59
        if (null === $this->integer8Reader) {
60
            $this->integer8Reader = new Integer8Reader($this->stream);
61
        }
62
63
        return $this->integer8Reader;
64
    }
65
66
    /**
67
     * Trim data.
68
     *
69
     * @param string $data The data to trim
70
     *
71
     * @return string
72
     */
73
    protected function trimData($data)
74
    {
75
        return trim(substr($data, 0, strcspn($data, "\x00")));
76
    }
77
78
    /**
79
     * Read ID3v1 tag version.
80
     *
81
     * @return int
82
     */
83
    protected function readVersion()
84
    {
85
        $this->stream->seek(-3, SEEK_END);
86
        if ("\x00" === $this->stream->read(1) && "\x00" !== $this->stream->read(1)) {
87
            return Version::VERSION_11;
88
        }
89
90
        return Version::VERSION_10;
91
    }
92
93
    /**
94
     * Read ID3v1 tag.
95
     *
96
     * @return null|TagInterface
97
     */
98
    public function read()
99
    {
100
        if (!$this->metadata->exists()) {
101
            return null;
102
        }
103
104
        $version = $this->readVersion();
105
        $tag = new Tag($version);
106
107
        $this->stream->seek(-125, SEEK_END);
108
        $tag
109
            ->setTitle($this->trimData($this->stream->read(30)))
110
            ->setArtist($this->trimData($this->stream->read(30)))
111
            ->setAlbum($this->trimData($this->stream->read(30)))
112
            ->setYear(intval($this->trimData($this->stream->read(4)), 10));
113
114
        if (Version::VERSION_11 === $version) {
115
            $tag->setComment($this->trimData($this->stream->read(28)));
116
            $this->stream->seek(1, SEEK_CUR);
117
            $tag->setTrack($this->getInteger8Reader()->read());
118
        } else {
119
            $tag->setComment($this->trimData($this->stream->read(30)));
120
        }
121
122
        $genre = $this->getInteger8Reader()->read();
123
        if (in_array($genre, Genre::values())) {
124
            $tag->setGenre($genre);
125
        }
126
127
        return $tag;
128
    }
129
}
130