AdvancedOutputMapping   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFormat() 0 3 1
A hasOut() 0 7 1
A __construct() 0 7 1
A getOutputMedia() 0 3 1
A apply() 0 13 5
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg;
4
5
use FFMpeg\Format\FormatInterface;
6
use FFMpeg\Format\Video\DefaultVideo;
7
use FFMpeg\Media\AdvancedMedia;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ProtoneMedia\LaravelFFMpeg\FFMpeg\AdvancedMedia. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Illuminate\Support\Collection;
9
use ProtoneMedia\LaravelFFMpeg\Exporters\HLSVideoFilters;
10
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
11
12
/**
13
 * This class is basically a wrapper around the map() method
14
 * of a the \FFMpeg\Media\AdvancedMedia class.
15
 */
16
class AdvancedOutputMapping
17
{
18
    /**
19
     * @var array
20
     */
21
    private $outs;
22
23
    /**
24
     * @var \FFMpeg\Format\FormatInterface
25
     */
26
    private $format;
27
28
    /**
29
     * @var \ProtoneMedia\LaravelFFMpeg\Filesystem\Media
30
     */
31
    private $output;
32
33
    /**
34
     * @var boolean
35
     */
36
    private $forceDisableAudio = false;
37
38
    /**
39
     * @var boolean
40
     */
41
    private $forceDisableVideo = false;
42
43
    public function __construct(array $outs, FormatInterface $format, Media $output, bool $forceDisableAudio = false, bool $forceDisableVideo = false)
44
    {
45
        $this->outs              = $outs;
46
        $this->format            = $format;
47
        $this->output            = $output;
48
        $this->forceDisableAudio = $forceDisableAudio;
49
        $this->forceDisableVideo = $forceDisableVideo;
50
    }
51
52
    /**
53
     * Applies the attributes to the format and specifies the video
54
     * bitrate if it's missing.
55
     */
56
    public function apply(AdvancedMedia $advancedMedia): void
57
    {
58
        if ($this->format instanceof DefaultVideo) {
59
            $parameters = $this->format->getAdditionalParameters() ?: [];
60
61
            if (!in_array('-b:v', $parameters) && $this->format->getKiloBitrate() !== 0) {
62
                $parameters = array_merge(['-b:v', $this->format->getKiloBitrate() . 'k'], $parameters);
63
            }
64
65
            $this->format->setAdditionalParameters($parameters);
66
        }
67
68
        $advancedMedia->map($this->outs, $this->format, $this->output->getLocalPath(), $this->forceDisableAudio, $this->forceDisableVideo);
69
    }
70
71
    public function getFormat(): FormatInterface
72
    {
73
        return $this->format;
74
    }
75
76
    public function getOutputMedia(): Media
77
    {
78
        return $this->output;
79
    }
80
81
    public function hasOut(string $out): bool
82
    {
83
        return Collection::make($this->outs)
84
            ->map(function ($out) {
85
                return HLSVideoFilters::beforeGlue($out);
86
            })
87
            ->contains(HLSVideoFilters::beforeGlue($out));
88
    }
89
}
90