Completed
Push — master ( c5eccd...ec8b64 )
by Evgenii
04:45
created

VideoFrameExtractor::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 15
rs 9.9666
cc 4
nc 4
nop 2
1
<?php
2
3
4
namespace floor12\files\logic;
5
6
use Yii;
7
8
class VideoFrameExtractor
9
{
10
    /** @var string */
11
    private $ffmpegBin;
12
    /** @var string */
13
    private $videoSourceFilename;
14
    /** @var string */
15
    private $outputImageFilename;
16
17
    public function __construct(string $videoSourceFilename, string $outputImageFilename)
18
    {
19
        $this->ffmpegBin = Yii::$app->getModule('files')->ffmpeg;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getModule('files')->ffmpeg can also be of type object. However, the property $ffmpegBin is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
20
21
        if (!file_exists($this->ffmpegBin))
0 ignored issues
show
Bug introduced by
It seems like $this->ffmpegBin can also be of type object; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

21
        if (!file_exists(/** @scrutinizer ignore-type */ $this->ffmpegBin))
Loading history...
22
            throw new \ErrorException("ffmpeg is not found: {$this->ffmpegBin}");
23
24
        if (!is_executable($this->ffmpegBin))
0 ignored issues
show
Bug introduced by
It seems like $this->ffmpegBin can also be of type object; however, parameter $filename of is_executable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

24
        if (!is_executable(/** @scrutinizer ignore-type */ $this->ffmpegBin))
Loading history...
25
            throw new \ErrorException("ffmpeg is not executable: {$this->ffmpegBin}");
26
27
        if (!is_file($videoSourceFilename))
28
            throw new \ErrorException('File not found on disk.');
29
30
        $this->videoSourceFilename = $videoSourceFilename;
31
        $this->outputImageFilename = $outputImageFilename;
32
    }
33
34
    /**
35
     * @throws \ErrorException
36
     */
37
    public function extract()
38
    {
39
        $command = "{$this->ffmpegBin} -i {$this->videoSourceFilename}  -ss 00:00:03.000 -vframes 1  {$this->outputImageFilename}";
40
        exec($command, $out, $result);
41
        if ($result !== 0)
42
            throw new \ErrorException('FFmpeg frame extracting fail:' . print_r($out, true));
43
    }
44
45
}
46