Completed
Push — id3-metadata-objects ( c855dc...168cf3 )
by Daniel
03:05
created

Metadata::write()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3

Importance

Changes 7
Bugs 1 Features 1
Metric Value
c 7
b 1
f 1
dl 0
loc 29
ccs 20
cts 20
cp 1
rs 8.8571
cc 3
eloc 19
nc 4
nop 1
crap 3
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\ID3v1;
9
10
use GravityMedia\Metadata\Exception\InvalidArgumentException;
11
use GravityMedia\Stream\Stream;
12
13
/**
14
 * ID3v1 metadata class.
15
 *
16
 * @package GravityMedia\Metadata\ID3v1
17
 */
18
class Metadata
19
{
20
    /**
21
     * The stream.
22
     *
23
     * @var Stream
24
     */
25
    protected $stream;
26
27
    /**
28
     * The filter.
29
     *
30
     * @var Filter
31
     */
32
    protected $filter;
33
34
    /**
35
     * Create ID3v1 metadata object from resource.
36
     *
37
     * @param resource $resource
38
     *
39
     * @throws InvalidArgumentException An exception will be thrown for invalid resource arguments.
40
     *
41
     * @return static
42
     */
43 24
    public static function fromResource($resource)
44
    {
45 24
        if (!is_resource($resource)) {
46 2
            throw new InvalidArgumentException('Invalid resource');
47
        }
48
49 22
        $stream = Stream::fromResource($resource);
50
51 22
        $metadata = new static();
52 22
        $metadata->stream = $stream;
53 22
        $metadata->filter = new Filter();
54
55 22
        return $metadata;
56
    }
57
58
    /**
59
     * Returns whether ID3v1 metadata exists.
60
     *
61
     * @return bool
62
     */
63 22 View Code Duplication
    public function exists()
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...
64
    {
65 22
        if ($this->stream->getSize() < 128) {
66 4
            return false;
67
        }
68
69 18
        $this->stream->seek(-128, SEEK_END);
70
71 18
        return 'TAG' === $this->stream->read(3);
72
    }
73
74
    /**
75
     * Strip ID3v1 metadata.
76
     *
77
     * @return $this
78
     */
79 4
    public function strip()
80
    {
81 4
        if (!$this->exists()) {
82 2
            return $this;
83
        }
84
85 2
        $this->stream->seek(0);
86 2
        $this->stream->truncate($this->stream->getSize() - 128);
87
88 2
        return $this;
89
    }
90
91
    /**
92
     * Read ID3v1 tag version.
93
     *
94
     * @return int
95
     */
96 4
    protected function readVersion()
97
    {
98 4
        $this->stream->seek(-3, SEEK_END);
99
100 4
        if ("\x00" === $this->stream->read(1) && "\x00" !== $this->stream->read(1)) {
101 2
            return Version::VERSION_11;
102
        }
103
104 2
        return Version::VERSION_10;
105
    }
106
107
    /**
108
     * Read ID3v1 tag.
109
     *
110
     * @return null|Tag
111
     */
112 6
    public function read()
113
    {
114 6
        if (!$this->exists()) {
115 2
            return null;
116
        }
117
118 4
        $version = $this->readVersion();
119 4
        $tag = new Tag($version);
120
121 4
        $this->stream->seek(-125, SEEK_END);
122 4
        $tag->setTitle($this->filter->decode($this->stream->read(30)));
123 4
        $tag->setArtist($this->filter->decode($this->stream->read(30)));
124 4
        $tag->setAlbum($this->filter->decode($this->stream->read(30)));
125 4
        $tag->setYear($this->filter->decode($this->stream->read(4)));
126
127 4
        if (Version::VERSION_11 === $version) {
128 2
            $tag->setComment($this->filter->decode($this->stream->read(28)));
129 2
            $this->stream->seek(1, SEEK_CUR);
130 2
            $tag->setTrack($this->stream->readUInt8());
131 1
        } else {
132 2
            $tag->setComment($this->filter->decode($this->stream->read(30)));
133
        }
134
135 4
        $genre = $this->stream->readUInt8();
136 4
        if (in_array($genre, Genre::values())) {
137 4
            $tag->setGenre($genre);
138 2
        }
139
140 4
        return $tag;
141
    }
142
143
    /**
144
     * Write ID3v1 tag.
145
     *
146
     * @param Tag $tag The tag to write.
147
     *
148
     * @return $this
149
     */
150 8
    public function write(Tag $tag)
151
    {
152 8
        $offset = 0;
153 8
        if ($this->exists()) {
154 4
            $offset = -128;
155 2
        }
156
157 8
        $this->stream->seek($offset, SEEK_END);
158
159 8
        $data = 'TAG';
160 8
        $data .= $this->filter->encode($tag->getTitle(), 30);
161 8
        $data .= $this->filter->encode($tag->getArtist(), 30);
162 8
        $data .= $this->filter->encode($tag->getAlbum(), 30);
163 8
        $data .= $this->filter->encode($tag->getYear(), 4);
164
165 8
        if (Version::VERSION_11 === $tag->getVersion()) {
166 4
            $data .= $this->filter->encode($tag->getComment(), 28);
167 4
            $data .= "\x00";
168 4
            $data .= $this->stream->writeUInt8($tag->getTrack());
169 2
        } else {
170 4
            $data .= $this->filter->encode($tag->getComment(), 30);
171
        }
172
173 8
        $data .= $this->stream->writeUInt8($tag->getGenre());
174
175 8
        $this->stream->write($data);
176
177 8
        return $this;
178
    }
179
}
180