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

Metadata::exists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 10
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
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\ID3v1\Reader\TagReader;
11
use GravityMedia\Metadata\ID3v1\Writer\TagWriter;
12
use GravityMedia\Metadata\Metadata\MetadataInterface;
13
use GravityMedia\Metadata\Metadata\TagInterface;
14
use GravityMedia\Stream\StreamInterface;
15
16
/**
17
 * ID3v1 metadata.
18
 *
19
 * @package GravityMedia\Metadata\ID3v1
20
 */
21
class Metadata implements MetadataInterface
22
{
23
    /**
24
     * @var StreamInterface
25
     */
26
    protected $stream;
27
28
    /**
29
     * @var TagReader
30
     */
31
    protected $tagReader;
32
33
    /**
34
     * @var TagWriter
35
     */
36
    protected $tagWriter;
37
38
    /**
39
     * Create ID3v1 metadata.
40
     *
41
     * @param StreamInterface $stream
42
     */
43
    public function __construct(StreamInterface $stream)
44
    {
45
        $this->stream = $stream;
46
    }
47
48
    /**
49
     * Get tag reader.
50
     *
51
     * @return TagReader
52
     */
53
    public function getTagReader()
54
    {
55
        if (null === $this->tagReader) {
56
            $this->tagReader = new TagReader($this, $this->stream);
57
        }
58
59
        return $this->tagReader;
60
    }
61
62
    /**
63
     * Get tag writer.
64
     *
65
     * @return TagWriter
66
     */
67
    public function getTagWriter()
68
    {
69
        if (null === $this->tagWriter) {
70
            $this->tagWriter = new TagWriter($this, $this->stream);
71
        }
72
73
        return $this->tagWriter;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 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...
80
    {
81
        if ($this->stream->getSize() < 128) {
82
            return false;
83
        }
84
85
        $this->stream->seek(-128, SEEK_END);
86
87
        return 'TAG' === $this->stream->read(3);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function strip()
94
    {
95
        if (!$this->exists()) {
96
            return $this;
97
        }
98
99
        $this->stream->seek(0);
100
        $this->stream->truncate($this->stream->getSize() - 128);
101
102
        return $this;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function read()
109
    {
110
        return $this->getTagReader()->read();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function write(TagInterface $tag)
117
    {
118
        $this->getTagWriter()->write($tag);
119
120
        return $this;
121
    }
122
}
123