Passed
Push — master ( 4422d3...1f3291 )
by Pascal
19:42 queued 15:59
created

LegacyFilterMapping::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 2
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\FFMpeg;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use ProtoneMedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use ProtoneMedia\LaravelFFMpeg\Filesystem\MediaCollection;
9
10
class LegacyFilterMapping
11
{
12
    private $in;
13
    private $out;
14
15
    /**
16
     * @var array
17
     */
18
    private $arguments;
19
20
    public function __construct($in, $out, ...$arguments)
21
    {
22
        $this->in        = $in;
23
        $this->out       = $out;
24
        $this->arguments = $arguments;
25
    }
26
27
    /**
28
     * Removes all non-numeric characters from the 'in' attribute.
29
     */
30
    public function normalizeIn(): int
31
    {
32
        return preg_replace("/[^0-9]/", "", $this->in);
33
    }
34
35
    /**
36
     * Filters the given MediaCollection down to one item by
37
     * guessing the key by the 'in' attribute.
38
     */
39
    private function singleMediaCollection(MediaCollection $mediaCollection): MediaCollection
40
    {
41
        $media = $mediaCollection->get($this->normalizeIn()) ?: $mediaCollection->first();
0 ignored issues
show
Bug introduced by
The method first() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

41
        $media = $mediaCollection->get($this->normalizeIn()) ?: $mediaCollection->/** @scrutinizer ignore-call */ first();
Loading history...
Bug introduced by
The method get() does not exist on ProtoneMedia\LaravelFFMp...esystem\MediaCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

41
        $media = $mediaCollection->/** @scrutinizer ignore-call */ get($this->normalizeIn()) ?: $mediaCollection->first();
Loading history...
42
43
        return MediaCollection::make([$media]);
44
    }
45
46
    /**
47
     * Applies the filter to the FFMpeg driver.
48
     */
49
    public function apply(PHPFFMpeg $driver, Collection $maps): void
50
    {
51
        $freshDriver = $driver->fresh()
52
            ->open($this->singleMediaCollection($driver->getMediaCollection()))
53
            ->addFilter(...$this->arguments);
54
55
        $format = $maps->filter->hasOut($this->out)->first()->getFormat();
56
57
        Collection::make($freshDriver->getFilters())->map(function ($filter) use ($freshDriver, $format) {
58
            $parameters = $filter->apply($freshDriver->get(), $format);
59
60
            $parameters = Arr::where($parameters, function ($parameter) {
61
                return !in_array($parameter, ['-vf', '-filter:v', '-filter_complex']);
62
            });
63
64
            return implode(' ', $parameters);
65
        })->each(function ($customCompiledFilter) use ($driver) {
66
            $driver->addFilter($this->in, $customCompiledFilter, $this->out);
67
        });
68
    }
69
}
70