Completed
Push — id3-metadata-objects ( 205ec7...3af9ca )
by Daniel
15:39
created

Metadata::decodeUnsynchronisation()   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 package.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Metadata\ID3v2;
9
10
use GravityMedia\Metadata\Exception\RuntimeException;
11
use GravityMedia\Metadata\ID3v2\Enum\Flag;
12
use GravityMedia\Metadata\ID3v2\Enum\Version;
13
use GravityMedia\Metadata\Metadata\MetadataInterface;
14
use GravityMedia\Metadata\Metadata\TagInterface;
15
use GravityMedia\Stream\StreamInterface;
16
use PhpBinaryReader\BinaryReader;
17
use PhpBinaryReader\Endian;
18
19
/**
20
 * ID3v2 metadata
21
 *
22
 * @package GravityMedia\Metadata\ID3v2
23
 */
24
class Metadata implements MetadataInterface
25
{
26
    /**
27
     * @var StreamInterface
28
     */
29
    protected $stream;
30
31
    /**
32
     * Create stream provider object from stream.
33
     *
34
     * @param StreamInterface $stream
35
     */
36
    public function __construct(StreamInterface $stream)
37
    {
38
        $this->stream = $stream;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 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...
45
    {
46
        if ($this->stream->getSize() < 10) {
47
            return false;
48
        }
49
50
        $this->stream->seek(0);
51
52
        return 'ID3' === $this->stream->read(3);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function strip()
59
    {
60
        return $this;
61
    }
62
63
    /**
64
     * Decode the given 28-bit synchsafe integer to regular 32-bit integer.
65
     *
66
     * @param int $data
67
     *
68
     * @return int
69
     */
70
    protected function decodeSynchsafe32($data)
71
    {
72
        return ($data & 0x7f) | ($data & 0x7f00) >> 1 |
73
        ($data & 0x7f0000) >> 2 | ($data & 0x7f000000) >> 3;
74
    }
75
76
    /**
77
     * Decode unsynchronisation.
78
     *
79
     * @param string $data
80
     *
81
     * @return string
82
     */
83
    protected function decodeUnsynchronisation($data)
84
    {
85
        return preg_replace('/\xff\x00\x00/', "\xff\x00", preg_replace('/\xff\x00(?=[\xe0-\xff])/', "\xff", $data));
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function read()
92
    {
93
        if (!$this->exists()) {
94
            return null;
95
        }
96
97
        $this->stream->seek(3);
98
99
        switch (ord($this->stream->read(1))) {
100
            case 2:
101
                $version = Version::VERSION_22;
102
                break;
103
            case 3:
104
                $version = Version::VERSION_23;
105
                break;
106
            case 4:
107
                $version = Version::VERSION_24;
108
                break;
109
            default:
110
                throw new RuntimeException('Invalid version.');
111
        }
112
113
        $header = new Header($version);
114
        $header->setRevision(ord($this->stream->read(1)));
115
116
        $flags = ord($this->stream->read(1));
117
        switch ($version) {
118
            case Version::VERSION_22:
119
                $header->setFlags([
120
                    Flag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
121
                    Flag::FLAG_COMPRESSION => (bool)($flags & 0x40)
122
                ]);
123
                break;
124 View Code Duplication
            case Version::VERSION_23:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
                $header->setFlags([
126
                    Flag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
127
                    Flag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40),
128
                    Flag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20)
129
                ]);
130
                break;
131 View Code Duplication
            case Version::VERSION_24:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
132
                $header->setFlags([
133
                    Flag::FLAG_UNSYNCHRONISATION => (bool)($flags & 0x80),
134
                    Flag::FLAG_EXTENDED_HEADER => (bool)($flags & 0x40),
135
                    Flag::FLAG_EXPERIMENTAL_INDICATOR => (bool)($flags & 0x20),
136
                    Flag::FLAG_FOOTER_PRESENT => (bool)($flags & 0x10)
137
                ]);
138
                break;
139
        }
140
141
        $size = new BinaryReader($this->stream->read(4), Endian::ENDIAN_BIG);
142
        $header->setSize($this->decodeSynchsafe32($size->readUInt32()));
143
144
        $tag = new Tag($header);
145
146
        if ($header->isFlagEnabled(Flag::FLAG_EXTENDED_HEADER)) {
147
            $extendedHeader = new ExtendedHeader();
148
149
            // TODO: Read extended header.
150
151
            $tag->setExtendedHeader($extendedHeader);
152
        }
153
154
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
155
        $size = $header->getSize();
156
        while ($size > 0) {
157
            switch ($version) {
158
                case Version::VERSION_22:
159
                    $frameName = $this->stream->read(3);
160
                    var_dump($frameName);
161
162
                    $frameSize = new BinaryReader($this->stream->read(3), Endian::ENDIAN_BIG);
163
                    var_dump($frameSize->readUInt32());
164
165
                    exit;
166
                    break;
167
            }
168
        }
169
170
        $data = $this->stream->read($header->getSize());
171
        var_dump($data);
172
        */
173
174
        return $tag;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function write(TagInterface $tag)
181
    {
182
        return $this;
183
    }
184
}
185