Failed Conditions
Push — master ( 45d676...509703 )
by Sébastien
05:56
created

VideoFilterChain   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 79
rs 10
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 3 1
A addFilters() 0 13 4
A addFilter() 0 3 1
A __construct() 0 7 2
A getFFmpegCLIValue() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Filter;
6
7
use Soluble\MediaTools\Common\Exception\UnsupportedParamValueException;
8
use Soluble\MediaTools\Video\Exception\InvalidArgumentException;
9
use Soluble\MediaTools\Video\Filter\Type\FFMpegVideoFilterInterface;
10
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface;
11
12
class VideoFilterChain implements FFMpegVideoFilterInterface
13
{
14
    /** @var VideoFilterInterface[] */
15
    protected $filters = [];
16 5
17
    /**
18 5
     * @param VideoFilterInterface[] $filters
19
     *
20 2
     * @throws InvalidArgumentException
21
     */
22 2
    public function __construct(array $filters = [])
23
    {
24
        if ($filters === []) {
25 5
            return;
26
        }
27 5
28 5
        $this->addFilters($filters);
29
    }
30
31
    /**
32
     * @return VideoFilterInterface[]
33 3
     */
34
    public function getFilters(): array
35 3
    {
36 3
        return $this->filters;
37 3
    }
38 1
39 1
    public function addFilter(VideoFilterInterface $filter): void
40 1
    {
41 1
        $this->filters[] = $filter;
42
    }
43
44
    /**
45 3
     * @param VideoFilterInterface[] $filters
46 3
     *
47 1
     * @throws InvalidArgumentException
48
     */
49
    public function addFilters(array $filters): void
50 3
    {
51
        foreach ($filters as $filter) {
52 2
            if (!$filter instanceof VideoFilterInterface) {
53
                throw new InvalidArgumentException(sprintf(
54
                    'Cannot add filter \'%s\', it must not implement %s',
55
                    is_object($filter) ? get_class($filter) : gettype($filter),
56 2
                    VideoFilterInterface::class
57
                ));
58
            }
59
            $this->filters[] = $filter;
60
        }
61
        $this->filters = $this->filters + $filters;
62
    }
63
64
    /**
65
     * @throws UnsupportedParamValueException
66
     */
67
    public function getFFmpegCLIValue(): string
68
    {
69
        $values = [];
70
        foreach ($this->filters as $filter) {
71
            if (!$filter instanceof FFMpegVideoFilterInterface) {
72
                throw new UnsupportedParamValueException(
73
                    sprintf(
74
                        'Filter \'%s\' have not been made compatible with FFMpeg',
75
                        get_class($filter)
76
                    )
77
                );
78
            }
79
            $val = $filter->getFFmpegCLIValue();
80
            if ($val === '') {
81
                continue;
82
            }
83
84
            $values[] = $val;
85
        }
86
        if (count($values) === 0) {
87
            return '';
88
        }
89
90
        return implode(',', $values);
91
    }
92
}
93