Completed
Pull Request — develop (#30)
by
unknown
02:43
created

Segments   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 58.2%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 139
ccs 39
cts 67
cp 0.582
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A key() 0 4 1
A next() 0 4 1
A valid() 0 4 1
A offsetSet() 0 12 4
A offsetGet() 0 4 1
A readLines() 0 21 4
A dump() 0 8 1
A offsetExists() 0 4 1
A offsetUnset() 0 4 1
A add() 0 4 1
A getDuration() 0 9 2
A getFirst() 0 7 2
A count() 0 4 1
A rewind() 0 4 1
A current() 0 4 1
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
    {
28 1
        $this->position = 0;
29 1
    }
30
31 1
    public function current()
32 1
    {
33 1
        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->segments[$offset] = $value;
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
    /**
73
     * @return bool
74
     */
75
    public function offsetExists($offset)
76
    {
77
        return isset($this->segments[$offset]);
78
    }
79
80
    public function offsetUnset($offset)
81
    {
82
        unset($this->segments[$offset]);
83
    }
84
85 1
    public function add(Segment $segment)
86
    {
87 1
        $this->segments[] = $segment;
88 1
    }
89
90
    /**
91
     * @return int|float
92
     */
93 1
    public function getDuration()
94
    {
95 1
        $duration = 0;
96 1
        foreach ($this->segments as $segment) {
97 1
            $duration += $segment->getDuration();
98 1
        }
99
100 1
        return $duration;
101
    }
102
103
    /**
104
     * @return Chrisyue\PhpM3u8\Segment
105
     */
106
    public function getFirst()
107
    {
108
        $first = reset($this->segments);
109
        if (false !== $first) {
110
            return $first;
111
        }
112
    }
113
114
    /**
115
     * @return int
116
     */
117
    public function count()
118
    {
119
        return count($this->segments);
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