Completed
Push — id3-metadata-objects ( 384c4c...c855dc )
by Daniel
02:54
created

Metadata   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 6.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 1 Features 1
Metric Value
wmc 15
c 11
b 1
f 1
lcom 1
cbo 4
dl 10
loc 145
ccs 61
cts 61
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A exists() 10 10 2
A strip() 0 11 2
A readVersion() 0 10 3
B read() 0 30 4
B write() 0 29 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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