Passed
Push — master ( 5da932...0ad23c )
by Amin
03:52
created

FFMpeg::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 1
c 2
b 2
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
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
use FFMpeg\Exception\ExceptionInterface;
15
use FFMpeg\FFMpeg as BFFMpeg;
16
use FFMpeg\FFProbe;
17
use Psr\Log\LoggerInterface;
18
use Streaming\Clouds\AWS;
19
use Streaming\Clouds\Cloud;
20
use Streaming\Clouds\CloudInterface;
21
use Streaming\Clouds\GoogleCloudStorage;
22
use Streaming\Clouds\MicrosoftAzure;
23
use Streaming\Exception\Exception;
24
use Streaming\Exception\InvalidArgumentException;
25
use Streaming\Exception\RuntimeException;
26
27
class FFMpeg
28
{
29
    /** @var BFFMpeg */
30
    protected $ffmpeg;
31
32
    /**
33
     * @param $ffmpeg
34
     */
35
    public function __construct(BFFMpeg $ffmpeg)
36
    {
37
        $this->ffmpeg = $ffmpeg;
38
    }
39
40
    /**
41
     * @param string $path
42
     * @param bool $is_tmp
43
     * @return Media
44
     */
45
    public function open(string $path, bool $is_tmp = false): Media
46
    {
47
        if (!is_file($path)) {
48
            throw new InvalidArgumentException("There is no file in this path: " . $path);
49
        }
50
51
        try {
52
            return new Media($this->ffmpeg->open($path), $path, $is_tmp);
53
        } catch (ExceptionInterface $e) {
54
            @unlink($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

54
            /** @scrutinizer ignore-unhandled */ @unlink($path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
55
            throw new RuntimeException(sprintf("There was an error opening this file: \n\n reason: \n %s", $e->getMessage()), $e->getCode(), $e);
56
        }
57
    }
58
59
    /**
60
     * @param array $cloud
61
     * @param string|null $save_to
62
     * @return Media
63
     * @throws Exception
64
     */
65
    public function openFromCloud(array $cloud, string $save_to = null): Media
66
    {
67
        list($is_tmp, $save_to) = $this->isTmp($save_to);
68
69
        if (is_array($cloud) && $cloud['cloud'] instanceof CloudInterface) {
70
            $cloud_obj = $cloud['cloud'];
71
            $options = (isset($cloud['options']) && is_array($cloud['options'])) ? $cloud['options'] : [];
72
73
            $cloud_obj->download($save_to, $options);
74
        } else {
75
            throw new InvalidArgumentException('You must pass an array of a cloud to the openFromCloud method. 
76
                    and the cloud must be instance of CloudInterface');
77
        }
78
79
        return $this->open($save_to, $is_tmp);
80
    }
81
82
    /**
83
     * @param $path
84
     * @return array
85
     * @throws Exception
86
     */
87
    private function isTmp($path)
88
    {
89
        $is_tmp = false;
90
91
        if (null === $path) {
92
            $is_tmp = true;
93
            $path = FileManager::tmpFile();
94
        }
95
96
        return [$is_tmp, $path];
97
    }
98
99
    /**
100
     * @param $method
101
     * @param $parameters
102
     * @return mixed
103
     */
104
    public function __call($method, $parameters)
105
    {
106
        return call_user_func_array([$this->ffmpeg, $method], $parameters);
107
    }
108
109
    /**
110
     * @param array $config
111
     * @param LoggerInterface $logger
112
     * @param FFProbe|null $probe
113
     * @return FFMpeg
114
     */
115
    public static function create($config = array(), LoggerInterface $logger = null, FFProbe $probe = null)
116
    {
117
        return new static(BFFMpeg::create($config, $logger, $probe));
118
    }
119
120
    /**
121
     * @param string $url
122
     * @param string|null $save_to
123
     * @param string $method
124
     * @param $request_options
125
     * @return Media
126
     * @throws Exception
127
     * @deprecated this method is deprecated
128
     */
129
    // @TODO: should be removed in the next releases.
130
    public function fromURL(string $url, string $save_to = null, string $method = "GET", array $request_options = []): Media
131
    {
132
        @trigger_error('fromURL method is deprecated and will be removed in a future release. Use Cloud instead', E_USER_DEPRECATED);
133
134
        Helper::isURL($url);
0 ignored issues
show
Deprecated Code introduced by
The function Streaming\Helper::isURL() has been deprecated: this method is deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

134
        /** @scrutinizer ignore-deprecated */ Helper::isURL($url);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
135
        list($is_tmp, $save_to) = $this->isTmp($save_to);
136
137
        $cloud = new Cloud($url, $method, $request_options);
138
        $cloud->download($save_to);
139
140
        return $this->open($save_to, $is_tmp);
141
    }
142
143
    /**
144
     * @param array $config
145
     * @param string $bucket
146
     * @param string $key
147
     * @param string|null $save_to
148
     * @return Media
149
     * @throws Exception
150
     * @deprecated this method is deprecated
151
     */
152
    // @TODO: should be removed in the next releases.
153
    public function fromS3(array $config, string $bucket, string $key, string $save_to = null): Media
154
    {
155
        @trigger_error('fromS3 method is deprecated and will be removed in a future release. Use AWS instead', E_USER_DEPRECATED);
156
157
        list($is_tmp, $save_to) = $this->isTmp($save_to);
158
159
        $aws = new AWS($config);
160
        $aws->download($save_to, ['bucket' => $bucket, 'key' => $key]);
161
162
        return $this->open($save_to, $is_tmp);
163
    }
164
165
    /**
166
     * @param array $config
167
     * @param string $bucket
168
     * @param string $name
169
     * @param string|null $save_to
170
     * @param bool $userProject
171
     * @return Media
172
     * @throws Exception
173
     * @deprecated this method is deprecated
174
     */
175
    // @TODO: should be removed in the next releases.
176
    public function fromGCS(array $config, string $bucket, string $name, string $save_to = null, $userProject = false): Media
177
    {
178
        @trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED);
179
        list($is_tmp, $save_to) = $this->isTmp($save_to);
180
181
        $google_cloud = new GoogleCloudStorage($config, $bucket, $userProject);
182
        $google_cloud->download($save_to, ['name' => $name]);
183
184
        return $this->open($save_to, $is_tmp);
185
    }
186
187
    /**
188
     * @param string $connectionString
189
     * @param string $container
190
     * @param string $blob
191
     * @param string|null $save_to
192
     * @return Media
193
     * @throws Exception
194
     * @deprecated this method is deprecated
195
     */
196
    // @TODO: should be removed in the next releases.
197
    public function fromMAS(string $connectionString, string $container, string $blob, string $save_to = null): Media
198
    {
199
        @trigger_error('fromMAS method is deprecated and will be removed in a future release. Use MicrosoftAzure instead', E_USER_DEPRECATED);
200
        list($is_tmp, $save_to) = $this->isTmp($save_to);
201
202
        $google_cloud = new MicrosoftAzure($connectionString);
203
        $google_cloud->download($save_to, ['container' => $container, 'blob' => $blob]);
204
205
        return $this->open($save_to, $is_tmp);
206
    }
207
}