Passed
Push — master ( c37170...fb5355 )
by Amin
02:31
created

HLSPlaylist   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
dl 0
loc 25
rs 10
c 1
b 1
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateContents() 0 9 2
A save() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the PHP-FFmpeg-video-streaming package.
5
 *
6
 * (c) Amin Yazdanpanah <[email protected]>
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 Streaming;
13
14
15
class HLSPlaylist
16
{
17
    /**
18
     * @param $filename
19
     * @param $representations
20
     */
21
    public static function save(string $filename, array $representations): void
22
    {
23
        file_put_contents($filename, static::generateContents($representations, $filename));
24
    }
25
26
    /**
27
     * @param array $representations
28
     * @param string $filename
29
     * @return string
30
     */
31
    private static function generateContents(array $representations, string $filename): string
32
    {
33
        $content = ["#EXTM3U", "#EXT-X-VERSION:3"];
34
        foreach ($representations as $rep) {
35
            $content[] = "#EXT-X-STREAM-INF:BANDWIDTH=" . $rep->getKiloBitrate() * 1024 . ",RESOLUTION=" . $rep->getResize();
36
            $content[] = pathinfo($filename, PATHINFO_FILENAME) . "_" . $rep->getHeight() . "p.m3u8";
37
        }
38
39
        return implode(PHP_EOL, $content);
40
    }
41
}