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

HLSKeyInfo::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
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
13
namespace Streaming;
14
15
16
use Streaming\Exception\RuntimeException;
17
18
class HLSKeyInfo
19
{
20
    /**
21
     * @param $url
22
     * @param $path
23
     * @param int $length
24
     * @return string
25
     */
26
    public static function generate(string $path, string $url, int $length = 16): string
27
    {
28
        if (!extension_loaded('openssl')) {
29
            throw new RuntimeException('OpenSSL is not installed.');
30
        }
31
32
        File::makeDir(dirname($path));
33
        file_put_contents($path, openssl_random_pseudo_bytes($length));
34
        file_put_contents($path_f = File::tmpFile(), implode(PHP_EOL, [$url, $path, bin2hex(openssl_random_pseudo_bytes($length))]));
35
36
        return $path_f;
37
    }
38
}