Passed
Push — master ( 3ee3fe...b14545 )
by Amin
04:48
created

KeyInfo::generateRandomKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 1
b 1
f 0
cc 1
nc 1
nop 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\Process\Process;
17
18
class KeyInfo
19
{
20
    private $url;
21
22
    private $path;
23
24
    private $path_info;
25
26
    private $openssl;
27
28
29
    /**
30
     * GenerateKeyInfo constructor.
31
     * @param $url
32
     * @param $path
33
     * @param string $binary
34
     * @throws Exception\Exception
35
     */
36
    public function __construct($url, $path, $binary = "openssl")
37
    {
38
        $this->url = $url;
39
        $this->path = $path;
40
        $this->path_info = pathinfo($path);
41
        $this->openssl = new Process($binary);
42
    }
43
44
    /**
45
     * @throws Exception\Exception
46
     */
47
    public function generate(): string
48
    {
49
        $key_info[] = $this->url;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$key_info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $key_info = array(); before regardless.
Loading history...
50
        $key_info[] = $this->generateRandomKey();
51
        $key_info[] = $this->generateIV();
52
53
        $key_info_path = $this->path_info["dirname"] . DIRECTORY_SEPARATOR . Helper::randomString() . ".keyinfo";
54
55
        file_put_contents($key_info_path, implode(PHP_EOL, $key_info));
56
57
        return $key_info_path;
58
    }
59
60
    /**
61
     * @return string
62
     * @throws Exception\Exception
63
     */
64
    public function __toString(): string
65
    {
66
        return $this->generate();
67
    }
68
69
    /**
70
     * @return string
71
     * @throws Exception\Exception
72
     */
73
    private function generateRandomKey(): string
74
    {
75
        Helper::makeDir($this->path_info["dirname"]);
76
        file_put_contents($this->path, $this->openssl->addCommand(['rand', '16'])->run());
77
78
        return $this->path;
79
    }
80
81
    /**
82
     * @return string
83
     * @throws Exception\Exception
84
     */
85
    private function generateIV(): string
86
    {
87
        return $this->openssl->removeCommand('16')->addCommand(['-hex' ,'16'])->run();
88
    }
89
}