VideoFilterChain::addFilter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @see       https://github.com/soluble-io/soluble-mediatools for the canonical repository
7
 *
8
 * @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude)
9
 * @license   https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT
10
 */
11
12
namespace Soluble\MediaTools\Video\Filter;
13
14
use Soluble\MediaTools\Common\Exception\UnsupportedParamValueException;
15
use Soluble\MediaTools\Video\Exception\InvalidArgumentException;
16
use Soluble\MediaTools\Video\Filter\Type\FFMpegVideoFilterInterface;
17
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface;
18
19
final class VideoFilterChain implements FFMpegVideoFilterInterface, \Countable
20
{
21
    /** @var VideoFilterInterface[] */
22
    private $filters = [];
23
24
    /**
25
     * @param VideoFilterInterface[] $filters
26
     *
27
     * @throws InvalidArgumentException
28
     */
29 25
    public function __construct(array $filters = [])
30
    {
31 25
        if ($filters === []) {
32 23
            return;
33
        }
34
35 4
        $this->addFilters($filters);
36 4
    }
37
38
    /**
39
     * @return VideoFilterInterface[]
40
     */
41 7
    public function getFilters(): array
42
    {
43 7
        return $this->filters;
44
    }
45
46
    /**
47
     * Will append the filter, if the filter is a VideoFilterChain
48
     * it will be merged at the end.
49
     *
50
     * @param VideoFilterInterface $filter filter to add
51
     */
52 19
    public function addFilter(VideoFilterInterface $filter): void
53
    {
54 19
        if ($filter instanceof self) {
55 5
            if ($filter->count() > 0) {
56 5
                $this->filters = array_merge($this->filters, $filter->getFilters());
57
            }
58
        } else {
59 17
            $this->filters[] = $filter;
60
        }
61 19
    }
62
63 6
    public function count(): int
64
    {
65 6
        return count($this->filters);
66
    }
67
68
    /**
69
     * @param VideoFilterInterface[] $filters
70
     *
71
     * @throws InvalidArgumentException
72
     */
73 8
    public function addFilters(array $filters): void
74
    {
75 8
        foreach ($filters as $filter) {
76 8
            if (!$filter instanceof VideoFilterInterface) {
77 2
                throw new InvalidArgumentException(sprintf(
78 2
                    'Cannot add filter \'%s\', it must not implement %s',
79 2
                    gettype($filter) === 'object' ? get_class($filter) : gettype($filter),
80 2
                    VideoFilterInterface::class
81
                ));
82
            }
83 8
            $this->addFilter($filter);
84
        }
85 6
    }
86
87
    /**
88
     * @throws UnsupportedParamValueException
89
     */
90 17
    public function getFFmpegCLIValue(): ?string
91
    {
92 17
        $values = [];
93 17
        foreach ($this->filters as $filter) {
94 10
            if (!$filter instanceof FFMpegVideoFilterInterface) {
95 1
                throw new UnsupportedParamValueException(
96 1
                    sprintf(
97 1
                        'Filter \'%s\' have not been made compatible with FFMpeg',
98 1
                        get_class($filter)
99
                    )
100
                );
101
            }
102 10
            $val = $filter->getFFmpegCLIValue();
103 10
            if ($val === '') {
104 1
                continue;
105
            }
106
107 10
            $values[] = $val;
108
        }
109
110 16
        if (count($values) === 0) {
111 7
            return null;
112
        }
113
114 9
        return implode(',', $values);
115
    }
116
}
117