Passed
Push — master ( d1ea3b...26546f )
by Sébastien
02:32
created

Info   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 113
ccs 39
cts 51
cp 0.7647
rs 10
c 0
b 0
f 0
wmc 19

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMetadata() 0 3 1
A getDuration() 0 3 1
A createFromFFProbeJson() 0 11 3
A getFile() 0 3 1
A getWidth() 0 5 1
A getNbFrames() 0 5 1
A getBitrate() 0 5 1
A getDimensions() 0 5 1
A getStreamsByType() 0 21 5
A getAudioStreamInfo() 0 3 1
A getVideoStreamInfo() 0 3 1
A getHeight() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Common\Exception\JsonParseException;
8
9
class Info implements InfoInterface
10
{
11
    public const STREAM_TYPE_AUDIO = 'audio';
12
    public const STREAM_TYPE_VIDEO = 'video';
13
    public const STREAM_TYPE_DATA  = 'data';
14
15
    /** @var array */
16
    protected $metadata;
17
18
    /** @var string */
19
    protected $file;
20
21 1
    public function __construct(string $file, array $metadata)
22
    {
23 1
        $this->metadata = $metadata;
24 1
        $this->file     = $file;
25 1
    }
26
27 1
    public static function createFromFFProbeJson(string $file, string $ffprobeJson): self
28
    {
29 1
        if (trim($ffprobeJson) === '') {
30
            throw new JsonParseException('Cannot parse empty json string');
31
        }
32 1
        $decoded = json_decode($ffprobeJson, true);
33 1
        if ($decoded === null) {
34
            throw new JsonParseException('Cannot parse json');
35
        }
36
37 1
        return new self($file, $decoded);
38
    }
39
40 1
    public function getFile(): string
41
    {
42 1
        return $this->file;
43
    }
44
45
    public function getMetadata(): array
46
    {
47
        return $this->metadata;
48
    }
49
50 1
    public function getDuration(): float
51
    {
52 1
        return (float) ($this->metadata['format']['duration'] ?? 0.0);
53
    }
54
55 1
    public function getDimensions(): array
56
    {
57
        return [
58 1
            'width'  => $this->getWidth(),
59 1
            'height' => $this->getHeight(),
60
        ];
61
    }
62
63 1
    public function getWidth(): int
64
    {
65 1
        $videoStream = $this->getVideoStreamInfo();
66
67 1
        return (int) ($videoStream['width'] ?? 0);
68
    }
69
70 1
    public function getHeight(): int
71
    {
72 1
        $videoStream = $this->getVideoStreamInfo();
73
74 1
        return (int) ($videoStream['height'] ?? 0);
75
    }
76
77 1
    public function getNbFrames(): int
78
    {
79 1
        $videoStream = $this->getVideoStreamInfo();
80
81 1
        return (int) ($videoStream['nb_frames'] ?? 0);
82
    }
83
84
    public function getBitrate(): int
85
    {
86
        $videoStream = $this->getVideoStreamInfo();
87
88
        return (int) ($videoStream['bit_rate'] ?? 0);
89
    }
90
91
    public function getAudioStreamInfo(): ?array
92
    {
93
        return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null;
94
    }
95
96 1
    public function getVideoStreamInfo(): ?array
97
    {
98 1
        return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null;
99
    }
100
101 1
    protected function getStreamsByType(): array
102
    {
103 1
        $streams = $this->metadata['streams'] ?? [];
104 1
        foreach ($streams as $stream) {
105 1
            $type = mb_strtolower($stream['codec_type']);
106
            switch ($type) {
107 1
                case self::STREAM_TYPE_VIDEO:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108 1
                    $streams['video'] = $stream;
109 1
                    break;
110 1
                case self::STREAM_TYPE_AUDIO:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
111 1
                    $streams['audio'] = $stream;
112 1
                    break;
113
                case self::STREAM_TYPE_DATA:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
                    $streams['data'] = $stream;
115
                    break;
116
                default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
117 1
                    throw new \Exception(sprintf('Does not support codec_type "%s"', $type));
118
            }
119
        }
120
121 1
        return $streams;
122
    }
123
}
124