Completed
Push — master ( e0e3f0...3077c0 )
by Sébastien
04:19
created

Info::getDimensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\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
    public function getDimensions(): array
56
    {
57
        $videoStream = $this->getVideoStreamInfo();
58
59
        return [
60
            'width'  => (int) ($videoStream['width'] ?? 0),
61
            'height' => (int) ($videoStream['height'] ?? 0),
62
        ];
63
    }
64
65 1
    public function getNbFrames(): int
66
    {
67 1
        $videoStream = $this->getVideoStreamInfo();
68
69 1
        return (int) ($videoStream['nb_frames'] ?? 0);
70
    }
71
72
    public function getBitrate(): int
73
    {
74
        $videoStream = $this->getVideoStreamInfo();
75
76
        return (int) ($videoStream['bit_rate'] ?? 0);
77
    }
78
79
    public function getAudioStreamInfo(): ?array
80
    {
81
        return $this->getStreamsByType()[self::STREAM_TYPE_AUDIO] ?? null;
82
    }
83
84 1
    public function getVideoStreamInfo(): ?array
85
    {
86 1
        return $this->getStreamsByType()[self::STREAM_TYPE_VIDEO] ?? null;
87
    }
88
89 1
    protected function getStreamsByType(): array
90
    {
91 1
        $streams = $this->metadata['streams'] ?? [];
92 1
        foreach ($streams as $stream) {
93 1
            $type = mb_strtolower($stream['codec_type']);
94
            switch ($type) {
95 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...
96 1
                    $streams['video'] = $stream;
97 1
                    break;
98 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...
99 1
                    $streams['audio'] = $stream;
100 1
                    break;
101
                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...
102
                    $streams['data'] = $stream;
103
                    break;
104
                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...
105 1
                    throw new \Exception(sprintf('Does not support codec_type "%s"', $type));
106
            }
107
        }
108
109 1
        return $streams;
110
    }
111
}
112