Passed
Push — master ( b23da5...cc1845 )
by Amin
04:10
created

HLSKeyInfo::updateSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
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 FFMpeg\Driver\FFMpegDriver;
17
use Streaming\Exception\RuntimeException;
18
19
class HLSKeyInfo
20
{
21
    /** @var string */
22
    private $path;
23
    /** @var string */
24
    private $c_path;
25
26
    /** @var string */
27
    private $url;
28
29
    /** @var string */
30
    private $c_url;
31
32
    /** @var string */
33
    private $key_info_path;
34
35
    /** @var string */
36
    private $suffix = "";
37
38
    /** @var string */
39
    private $search_for;
40
41
    /** @var int */
42
    private $length = 16;
43
44
    /** @var int */
45
    private $key_rotation_period;
46
47
    /** @var array */
48
    private $segments = [];
49
50
    /**
51
     * HLSKeyInfo constructor.
52
     * @param string $path
53
     * @param string $url
54
     */
55
    public function __construct(string $path, string $url)
56
    {
57
        $this->path = $this->c_path = $path;
58
        $this->url = $this->c_url = $url;
59
        File::makeDir(dirname($path));
60
        $this->key_info_path = File::tmp();
61
    }
62
63
    /**
64
     * @param string $path
65
     * @param string $url
66
     * @return HLSKeyInfo
67
     */
68
    public static function create(string $path, string $url): HLSKeyInfo
69
    {
70
        return new static($path, $url);
71
    }
72
73
    /**
74
     * Generate a encryption key
75
     */
76
    public function createKey(): void
77
    {
78
        if (!extension_loaded('openssl')) {
79
            throw new RuntimeException('OpenSSL is not installed.');
80
        }
81
82
        File::put($this->path, openssl_random_pseudo_bytes($this->length));
83
    }
84
85
    /**
86
     * update or generate a key info file
87
     */
88
    public function createKeyInfo(): void
89
    {
90
        $content = implode(
91
            PHP_EOL,
92
            [
93
                $this->url,
94
                $this->path,
95
                bin2hex(openssl_random_pseudo_bytes($this->length))
96
            ]
97
        );
98
        File::put($this->key_info_path, $content);
99
    }
100
101
    /**
102
     * @param FFMpegDriver $driver
103
     * @param int $key_rotation_period
104
     * @param string $search_for
105
     */
106
    public function rotateKey(FFMpegDriver $driver, int $key_rotation_period, string $search_for): void
107
    {
108
        $this->key_rotation_period = $key_rotation_period;
109
        $this->search_for = $search_for;
110
111
        call_user_func_array([$driver->listen(new FFMpegListener), 'on'], ['listen', $this->call()]);
112
    }
113
114
    /**
115
     * @return void
116
     */
117
    public function generate(): void
118
    {
119
        $this->createKey();
120
        $this->createKeyInfo();
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function __toString()
127
    {
128
        $this->generate();
129
        return $this->key_info_path;
130
    }
131
132
    /**
133
     * @param string $key_info_path
134
     * @return HLSKeyInfo
135
     */
136
    public function setKeyInfoPath(string $key_info_path): HLSKeyInfo
137
    {
138
        $this->key_info_path = $key_info_path;
139
        return $this;
140
    }
141
142
    /**
143
     * @param int $length
144
     * @return HLSKeyInfo
145
     */
146
    public function setLength(int $length): HLSKeyInfo
147
    {
148
        $this->length = $length;
149
        return $this;
150
    }
151
152
    /**
153
     * @param string $suffix
154
     * @return HLSKeyInfo
155
     */
156
    public function setSuffix(string $suffix): HLSKeyInfo
157
    {
158
        $this->suffix = $suffix;
159
        return $this;
160
    }
161
162
    /**
163
     * update the suffix of paths
164
     */
165
    private function updateSuffix()
166
    {
167
        $suffix = uniqid("_") . $this->suffix;
168
169
        $this->path = $this->c_path . $suffix;
170
        $this->url = $this->c_url . $suffix;
171
    }
172
173
    /**
174
     * check if the new segment is created or not
175
     * @return callable
176
     */
177
    private function call(): callable
178
    {
179
        return function ($log) {
180
            if (false !== strpos($log, $this->search_for) && !in_array($log, $this->segments)) {
181
                array_push($this->segments, $log);
182
                if ($this->key_rotation_period === 1 || (count($this->segments) % $this->key_rotation_period) === 0) {
183
                    $this->updateSuffix();
184
                    $this->generate();
185
                }
186
            }
187
        };
188
    }
189
}
190