Passed
Push — master ( 5a13b9...a6a132 )
by Pascal
02:03
created

InteractsWithFilters::addFilter()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 36
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 36
rs 9.2222
1
<?php
2
3
namespace ProtoneMedia\LaravelFFMpeg\Drivers;
4
5
use Closure;
6
use FFMpeg\Coordinate\Dimension;
7
use FFMpeg\Filters\Audio\SimpleFilter;
8
use FFMpeg\Filters\FilterInterface;
9
use FFMpeg\Filters\Video\ResizeFilter;
10
use Illuminate\Support\Collection;
11
use ProtoneMedia\LaravelFFMpeg\Exporters\MediaExporter;
12
use ProtoneMedia\LaravelFFMpeg\FFMpeg\LegacyFilterMapping;
13
use ProtoneMedia\LaravelFFMpeg\Filesystem\Media;
14
use ProtoneMedia\LaravelFFMpeg\Filters\WatermarkFactory;
15
16
trait InteractsWithFilters
17
{
18
    /**
19
     * @var \Illuminate\Support\Collection
20
     */
21
    protected $pendingComplexFilters;
22
23
    /**
24
     * Returns an array with the filters applied to the underlying media object.
25
     *
26
     * @return array
27
     */
28
    public function getFilters(): array
29
    {
30
        return iterator_to_array($this->media->getFiltersCollection());
31
    }
32
33
    /**
34
     * Helper method to provide multiple ways to add a filter to the underlying
35
     * media object.
36
     *
37
     * @return self
38
     */
39
    public function addFilter(): self
40
    {
41
        $arguments = func_get_args();
42
43
        // to support '[in]filter[out]' complex filters
44
        if ($this->isAdvancedMedia() && count($arguments) === 3) {
0 ignored issues
show
Bug introduced by
It seems like isAdvancedMedia() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

44
        if ($this->/** @scrutinizer ignore-call */ isAdvancedMedia() && count($arguments) === 3) {
Loading history...
45
            $this->media->filters()->custom(...$arguments);
46
47
            return $this;
48
        }
49
50
        // use a callback to add a filter
51
        if ($arguments[0] instanceof Closure) {
52
            call_user_func_array($arguments[0], [$this->media->filters()]);
53
54
            return $this;
55
        }
56
57
        // use an object to add a filter
58
        if ($arguments[0] instanceof FilterInterface) {
59
            call_user_func_array([$this->media, 'addFilter'], $arguments);
60
61
            return $this;
62
        }
63
64
        // use a single array with parameters to define a filter
65
        if (is_array($arguments[0])) {
66
            $this->media->addFilter(new SimpleFilter($arguments[0]));
67
68
            return $this;
69
        }
70
71
        // use all function arguments as a filter
72
        $this->media->addFilter(new SimpleFilter($arguments));
73
74
        return $this;
75
    }
76
77
    /**
78
     * Calls the callable with a WatermarkFactory instance and
79
     * adds the freshly generated WatermarkFilter.
80
     *
81
     * @param callable $withWatermarkFactory
82
     * @return self
83
     */
84
    public function addWatermark(callable $withWatermarkFactory): self
85
    {
86
        $withWatermarkFactory(
87
            $watermarkFactory = new WatermarkFactory
88
        );
89
90
        return $this->addFilter($watermarkFactory->get());
91
    }
92
93
    /**
94
     * Shortcut for adding a Resize filter.
95
     *
96
     * @param int $width
97
     * @param int $height
98
     * @param string $mode
99
     * @return self
100
     */
101
    public function resize($width, $height, $mode = ResizeFilter::RESIZEMODE_FIT): self
102
    {
103
        $dimension = new Dimension($width, $height);
104
105
        $filter = new ResizeFilter($dimension, $mode);
106
107
        return $this->addFilter($filter);
108
    }
109
110
    /**
111
     * Maps the arguments into a 'LegacyFilterMapping' instance and
112
     * pushed it to the 'pendingComplexFilters' collection. These
113
     * filters will be applied later on by the MediaExporter.
114
     */
115
    public function addFilterAsComplexFilter($in, $out, ...$arguments): self
116
    {
117
        $this->pendingComplexFilters->push(new LegacyFilterMapping(
118
            $in,
119
            $out,
120
            ...$arguments
121
        ));
122
123
        return $this;
124
    }
125
126
    /**
127
     * Getter for the pending complex filters.
128
     *
129
     * @return \Illuminate\Support\Collection
130
     */
131
    public function getPendingComplexFilters(): Collection
132
    {
133
        return $this->pendingComplexFilters;
134
    }
135
}
136