Completed
Push — develop ( dba330...cda2f3 )
by Chris
02:01
created

Dumper::createByteRangeLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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
use Chrisyue\PhpM3u8\M3u8\M3u8;
15
use Chrisyue\PhpM3u8\M3u8\MediaSegment;
16
17
class Dumper
18
{
19 2
    public function dump(M3u8 $m3u8)
20
    {
21
        $lines = array(
22 2
            '#EXTM3U',
23 2
            sprintf('#EXT-X-VERSION:%s', $m3u8->getVersion()),
24 2
            sprintf('#EXT-X-TARGETDURATION:%d', $m3u8->getTargetDuration()),
25 2
        );
26
27 2
        if ($m3u8->getMediaSequence()) {
28 2
            $lines[] = sprintf('#EXT-X-MEDIA-SEQUENCE:%s', $m3u8->getMediaSequence());
29 2
        }
30
31 2
        if ($m3u8->getDiscontinuitySequence()) {
32
            $lines[] = sprintf('#EXT-X-DISCONTINUITY-SEQUENCE:%s', $m3u8->getDiscontinuitySequence());
33
        }
34
35 2
        foreach ($m3u8->getPlaylist() as $mediaSegment) {
36 2
            if ($mediaSegment->isDiscontinuity()) {
37 2
                $lines[] = '#EXT-X-DISCONTINUITY';
38 2
            }
39
40 2
            $lines[] = self::createExtinfLine($m3u8->getVersion(), $mediaSegment);
41
42 2
            $byteRangeline = self::createByteRangeLine($mediaSegment);
43 2
            if (!empty($byteRangeline)) {
44 2
                $lines[] = $byteRangeline;
45 2
            }
46
47 2
            $lines[] = $mediaSegment->getUri();
48 2
        }
49
50 2
        return implode(PHP_EOL, $lines);
51
    }
52
53 2
    private static function createExtinfLine($m3u8Version, MediaSegment $mediaSegment)
54
    {
55 2
        if ($m3u8Version < 3) {
56 1
            return sprintf('#EXTINF:%d,%s', round($mediaSegment->getDuration()), $mediaSegment->getTitle());
57
        }
58
59 1
        return sprintf('#EXTINF:%.3f,%s', $mediaSegment->getDuration(), $mediaSegment->getTitle());
60
    }
61
62 2
    private static function createByteRangeLine(MediaSegment $mediaSegment)
63
    {
64 2
        $byteRange = $mediaSegment->getByteRange();
65 2
        if (empty($byteRange[0])) {
66 2
            return;
67
        }
68
69 2
        $line = sprintf('#EXT-X-BYTERANGE:%d', $byteRange[0]);
70 2
        if (empty($byteRange[1])) {
71 2
            return $line;
72
        }
73
74 2
        return sprintf('%s@%d', $line, $byteRange[1]);
75
    }
76
}
77