Completed
Push — id3-metadata-objects ( 3a53b8...e55453 )
by Daniel
14:15
created

Metadata   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 9.17 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 8
Bugs 0 Features 2
Metric Value
wmc 13
c 8
b 0
f 2
lcom 1
cbo 10
dl 10
loc 109
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A exists() 10 10 2
A strip() 0 4 1
B read() 0 58 8
A write() 0 4 1

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\ID3v2;
9
10
use GravityMedia\Metadata\ID3v2\Converter\Unsynchronisation;
11
use GravityMedia\Metadata\ID3v2\Enum\HeaderFlag;
12
use GravityMedia\Metadata\ID3v2\Metadata\ExtendedHeaderReader;
13
use GravityMedia\Metadata\ID3v2\Metadata\FrameReader;
14
use GravityMedia\Metadata\ID3v2\Metadata\HeaderReader;
15
use GravityMedia\Metadata\Metadata\MetadataInterface;
16
use GravityMedia\Metadata\Metadata\TagInterface;
17
use GravityMedia\Stream\Stream;
18
use GravityMedia\Stream\StreamInterface;
19
20
/**
21
 * ID3v2 metadata
22
 *
23
 * @package GravityMedia\Metadata\ID3v2
24
 */
25
class Metadata implements MetadataInterface
26
{
27
    /**
28
     * @var StreamInterface
29
     */
30
    protected $stream;
31
32
    /**
33
     * Create ID3v2 metadata object.
34
     *
35
     * @param StreamInterface $stream
36
     */
37
    public function __construct(StreamInterface $stream)
38
    {
39
        $this->stream = $stream;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 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
        if ($this->stream->getSize() < 10) {
48
            return false;
49
        }
50
51
        $this->stream->seek(0);
52
53
        return 'ID3' === $this->stream->read(3);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function strip()
60
    {
61
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function read()
68
    {
69
        if (!$this->exists()) {
70
            return null;
71
        }
72
73
        $headerReader = new HeaderReader($this->stream);
74
75
        $this->stream->seek(3);
76
        $header = $headerReader->read();
77
78
        $tag = new Tag($header);
79
80
        $data = $this->stream->read($header->getSize());
81
        if ($header->isFlagEnabled(HeaderFlag::FLAG_COMPRESSION)) {
82
            $data = gzuncompress($data);
83
        }
84
85
        if ($header->isFlagEnabled(HeaderFlag::FLAG_UNSYNCHRONISATION)) {
86
            $data = Unsynchronisation::decode($data);
87
        }
88
89
        $resource = fopen('php://temp', 'r+b');
90
        $stream = Stream::fromResource($resource);
91
        $stream->write($data);
92
        $stream->rewind();
93
94
        $size = $stream->getSize();
95
96
        if ($header->isFlagEnabled(HeaderFlag::FLAG_EXTENDED_HEADER)) {
97
            $extendedHeaderReader = new ExtendedHeaderReader($stream, $header);
98
            $extendedHeader = $extendedHeaderReader->read();
99
100
            $tag->setExtendedHeader($extendedHeader);
101
102
            $size -= $extendedHeader->getSize();
103
        }
104
105
        if ($header->isFlagEnabled(HeaderFlag::FLAG_EXTENDED_HEADER)) {
106
            // TODO: Read footer from stream
107
108
            $size -= 10;
109
        }
110
111
        $frameReader = new FrameReader($stream, $header);
112
        while ($size > 0) {
113
            $frame = $frameReader->read();
114
            if (0 === $frame->getSize()) {
115
                break;
116
            }
117
118
            $tag->addFrame($frame);
119
120
            $size -= $frame->getSize();
121
        }
122
123
        return $tag;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function write(TagInterface $tag)
130
    {
131
        return $this;
132
    }
133
}
134