Completed
Branch v7 (f01542)
by Pascal
08:46
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
eloc 11
c 0
b 0
f 0
dl 0
loc 18
rs 9.9
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Pbmedia\LaravelFFMpeg\FFMpeg;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Collection;
7
use Pbmedia\LaravelFFMpeg\Drivers\PHPFFMpeg;
8
use Pbmedia\LaravelFFMpeg\Filesystem\MediaCollection;
9
10
class LegacyFilterMapping
11
{
12
    private $in;
13
    private $out;
14
    private array $arguments;
15
16
    public function __construct($in, $out, ...$arguments)
17
    {
18
        $this->in        = $in;
19
        $this->out       = $out;
20
        $this->arguments = $arguments;
21
    }
22
23
    /**
24
     * Removes all non-numeric characters from the 'in' attribute.
25
     */
26
    public function normalizeIn(): int
27
    {
28
        return preg_replace("/[^0-9]/", "", $this->in);
29
    }
30
31
    /**
32
     * Filters the given MediaCollection down to one item by
33
     * guessing the key by the 'in' attribute.
34
     */
35
    private function singleMediaCollection(MediaCollection $mediaCollection): MediaCollection
36
    {
37
        $media = $mediaCollection->get($this->normalizeIn()) ?: $mediaCollection->first();
0 ignored issues
show
Bug introduced by
The method get() does not exist on Pbmedia\LaravelFFMpeg\Filesystem\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

37
        $media = $mediaCollection->/** @scrutinizer ignore-call */ get($this->normalizeIn()) ?: $mediaCollection->first();
Loading history...
Bug introduced by
The method first() does not exist on Pbmedia\LaravelFFMpeg\Filesystem\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

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