Passed
Push — master ( 4fb215...21aaac )
by Sébastien
03:38
created

VideoFilterChain::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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, \Countable
13
{
14
    /** @var VideoFilterInterface[] */
15
    private $filters = [];
16
17
    /**
18
     * @param VideoFilterInterface[] $filters
19
     *
20
     * @throws InvalidArgumentException
21
     */
22 17
    public function __construct(array $filters = [])
23
    {
24 17
        if ($filters === []) {
25 15
            return;
26
        }
27
28 4
        $this->addFilters($filters);
29 4
    }
30
31
    /**
32
     * @return VideoFilterInterface[]
33
     */
34 10
    public function getFilters(): array
35
    {
36 10
        return $this->filters;
37
    }
38
39 9
    public function addFilter(VideoFilterInterface $filter): void
40
    {
41 9
        $this->filters[] = $filter;
42 9
    }
43
44 1
    public function count(): int
45
    {
46 1
        return count($this->filters);
47
    }
48
49
    /**
50
     * @param VideoFilterInterface[] $filters
51
     *
52
     * @throws InvalidArgumentException
53
     */
54 7
    public function addFilters(array $filters): void
55
    {
56 7
        foreach ($filters as $filter) {
57 7
            if (!$filter instanceof VideoFilterInterface) {
58 2
                throw new InvalidArgumentException(sprintf(
59 2
                    'Cannot add filter \'%s\', it must not implement %s',
60 2
                    is_object($filter) ? get_class($filter) : gettype($filter),
61 2
                    VideoFilterInterface::class
62
                ));
63
            }
64 7
            $this->filters[] = $filter;
65
        }
66 5
        $this->filters = $this->filters + $filters;
67 5
    }
68
69
    /**
70
     * @throws UnsupportedParamValueException
71
     */
72 7
    public function getFFmpegCLIValue(): string
73
    {
74 7
        $values = [];
75 7
        foreach ($this->filters as $filter) {
76 7
            if (!$filter instanceof FFMpegVideoFilterInterface) {
77 1
                throw new UnsupportedParamValueException(
78 1
                    sprintf(
79 1
                        'Filter \'%s\' have not been made compatible with FFMpeg',
80 1
                        get_class($filter)
81
                    )
82
                );
83
            }
84 7
            $val = $filter->getFFmpegCLIValue();
85 7
            if ($val === '') {
86 1
                continue;
87
            }
88
89 7
            $values[] = $val;
90
        }
91 6
        if (count($values) === 0) {
92
            return '';
93
        }
94
95 6
        return implode(',', $values);
96
    }
97
}
98