Completed
Pull Request — develop (#19)
by Chris
02:04
created

Segments::readLines()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 4.0039
1
<?php
2
3
/*
4
 * This file is part of the PhpM3u8 package.
5
 *
6
 * (c) Chrisyue <http://chrisyue.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Chrisyue\PhpM3u8;
13
14
class Segments implements DumpableInterface, \Iterator, \ArrayAccess
15
{
16
    private $segments = [];
17
    private $m3u8;
18
19
    private $position = 0;
20
21 2
    public function __construct(M3u8 $m3u8)
22
    {
23 2
        $this->m3u8 = $m3u8;
24 2
    }
25
26 1
    public function rewind()
27 1
    {
28 1
        $this->position = 0;
29 1
    }
30
31
    public function current()
32
    {
33
        return $this->segments[$this->position];
34
    }
35
36
    public function key()
37
    {
38
        return $this->position;
39
    }
40
41
    public function next()
42
    {
43
        ++$this->position;
44
    }
45
46
    public function valid()
47
    {
48
        return isset($this->segments[$this->position]);
49
    }
50
51
    public function offsetSet($offset, $value)
52
    {
53
        if (!is_object($value) || !$value instanceof Segment) {
54
            throw new \InvalidArgumentException('Expected $value of class Chrisyue\PhpM3u8\Segment');
55
        }
56
        if (is_null($offset)) {
57
            $this->add($value);
58
59
            return;
60
        }
61
        $this->mediaSegments[$offset] = $value;
0 ignored issues
show
Bug introduced by
The property mediaSegments does not seem to exist. Did you mean segments?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
    }
63
64
    /**
65
     * @return Chrisyue\PhpM3u8\Segment
66
     */
67 1
    public function offsetGet($offset)
68
    {
69 1
        return $this->segments[$offset];
70
    }
71
72
    public function offsetExists($offset)
73
    {
74
        return isset($this->mediaSegments[$offset]);
0 ignored issues
show
Bug introduced by
The property mediaSegments does not seem to exist. Did you mean segments?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75
    }
76
77
    public function offsetUnset($offset)
78
    {
79
        unset($this->mediaSegments[$offset]);
80
    }
81
82 1
    public function add(Segment $segment)
83
    {
84 1
        $this->segments[] = $segment;
85 1
    }
86
87
    public function getDuration()
88
    {
89
        $duration = 0;
90
        foreach ($this->segments as $segment) {
91
            $duration += $segment->getDuration();
92
        }
93
94
        return $duration;
95
    }
96
97
    public function getFirst()
98
    {
99
        $first = reset($this->segments);
100
        if (false !== $first) {
101
            return $first;
102
        }
103
    }
104
105
    public function count()
106
    {
107
        return count($this->segments);
108
    }
109
110
    public function setAge($age)
111
    {
112
        $this->age = $age;
0 ignored issues
show
Bug introduced by
The property age does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
113
114
        return $this;
115
    }
116
117
    public function getAge()
118
    {
119
        return $this->age;
120
    }
121
122 1
    public function readLines(array &$lines)
123
    {
124 1
        $mediaSequence = $this->m3u8->getMediaSequence();
125 1
        $discontinuitySequence = $this->m3u8->getDiscontinuitySequence();
126 1
        while (true) {
127 1
            $segment = new Segment($this->m3u8->getVersion());
128 1
            $segment->readLines($lines);
129
130 1
            if ($segment->isEmpty()) {
131 1
                return;
132
            }
133
134 1
            $segment->setMediaSequence($mediaSequence++);
135
136 1
            $segment->setDiscontinuitySequence($discontinuitySequence);
137 1
            if ($segment->isDiscontinuity()) {
138 1
                $segment->setDiscontinuitySequence(++$discontinuitySequence);
139 1
            }
140 1
            $this->segments[] = $segment;
141 1
        }
142
    }
143
144
    public function dump()
145
    {
146 1
        $lines = array_map(function (Segment $segment) {
147 1
            return $segment->dump();
148 1
        }, $this->segments);
149
150 1
        return implode("\n", $lines);
151
    }
152
}
153