Passed
Push — master ( 1b4300...336f0f )
by Amin
02:54 queued 10s
created

AutoRepresentations::getKiloBitRates()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 2
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 Streaming\Exception\InvalidArgumentException;
15
use Streaming\MediaInfo\Streams\Stream;
16
use Streaming\MediaInfo\Streams\StreamCollection;
17
18
class AutoRepresentations
19
{
20
    /** @var Stream $video */
21
    private $video;
22
23
    /** @var Stream $general */
24
    private $general;
25
26
    /**
27
     * regular video's heights
28
     *
29
     * @var array side_values
30
     */
31
    private $side_values = [2160, 1080, 720, 480, 360, 240, 144];
32
33
    /** @var array $k_bitrate_values */
34
    private $k_bitrate_values;
35
36
    /**
37
     * AutoRepresentations constructor.
38
     * @param StreamCollection $streamCollection
39
     * @param null | array $side_values
40
     * @param array $k_bitrate_values
41
     */
42
    public function __construct(StreamCollection $streamCollection, array $side_values = null, array $k_bitrate_values = null)
43
    {
44
        $this->video = $streamCollection->videos()->first();
45
        $this->general = $streamCollection->general();
46
        $this->getSideValues($side_values);
47
        $this->getKiloBitrateValues($k_bitrate_values);
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    private function getDimensions(): array
54
    {
55
        $width = $this->video->get('Width');
0 ignored issues
show
Bug introduced by
'Width' of type string is incompatible with the type array expected by parameter $attr of Streaming\MediaInfo\Streams\Stream::get(). ( Ignorable by Annotation )

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

55
        $width = $this->video->get(/** @scrutinizer ignore-type */ 'Width');
Loading history...
56
        $height = $this->video->get('Height');
57
58
        return [$width, $height, $width / $height];
59
    }
60
61
    /**
62
     * @return mixed
63
     * @throws InvalidArgumentException
64
     */
65
    private function getKiloBitRate(): int
66
    {
67
        if (!$this->video->has('BitRate')) {
68
            if (!$this->general->has('OverallBitRate')) {
69
                throw new InvalidArgumentException("Invalid stream");
70
            }
71
72
            return intval(($this->general->get('OverallBitRate') / 1024) * .9);
0 ignored issues
show
Bug introduced by
'OverallBitRate' of type string is incompatible with the type array expected by parameter $attr of Streaming\MediaInfo\Streams\Stream::get(). ( Ignorable by Annotation )

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

72
            return intval(($this->general->get(/** @scrutinizer ignore-type */ 'OverallBitRate') / 1024) * .9);
Loading history...
73
        }
74
75
        return (int)$this->video->get('BitRate') / 1024;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function get(): array
82
    {
83
        $k_bitrate = $this->getKiloBitRate();
84
        list($w, $h, $r) = $this->getDimensions();
85
86
        $reps[] = $this->addRep($k_bitrate, $w, $h);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$reps was never initialized. Although not strictly required by PHP, it is generally a good practice to add $reps = array(); before regardless.
Loading history...
87
88
        foreach ($this->side_values as $key => $height) {
89
            $reps[] = $this->addRep($this->k_bitrate_values[$key], Helper::roundToEven($r * $height), $height);
90
        }
91
92
        return array_reverse($reps);
93
    }
94
95
    /**
96
     * @param $width
97
     * @param $k_bitrate
98
     * @param $height
99
     * @return Representation
100
     * @throws InvalidArgumentException
101
     */
102
    private function addRep($k_bitrate, $width, $height): Representation
103
    {
104
        return (new Representation())->setKiloBitrate($k_bitrate)->setResize($width, $height);
105
    }
106
107
    /**
108
     * @param array|null $k_bitrate_values
109
     */
110
    private function getKiloBitrateValues(?array $k_bitrate_values): void
111
    {
112
        $count_sides = count($this->side_values);
113
114
        if ($k_bitrate_values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $k_bitrate_values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
115
            if ($count_sides !== count($k_bitrate_values)) {
116
                throw new InvalidArgumentException("The count of side value array must be the same as the count of kilo bitrate array");
117
            }
118
119
            $this->k_bitrate_values = $k_bitrate_values;
120
            return;
121
        }
122
123
        $k_bitrate_value = $this->getKiloBitRate();
124
        $divided_by = 1.5;
125
126
        while ($count_sides) {
127
            $this->k_bitrate_values[] = (($k_bitrate = intval($k_bitrate_value / $divided_by)) < 64) ? 64 : $k_bitrate;
128
            $divided_by += .5;
129
            $count_sides--;
130
        }
131
    }
132
133
    /**
134
     * @param array|null $side_values
135
     */
136
    private function getSideValues(?array $side_values): void
137
    {
138
        if ($side_values) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $side_values of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
139
            $this->side_values = $side_values;
140
            return;
141
        }
142
143
        $h = $this->getDimensions()[1];
144
145
        $this->side_values = array_values(array_filter($this->side_values, function ($height) use ($h) {
146
            return $height < $h;
147
        }));
148
    }
149
}