Failed Conditions
Push — master ( 16370c...4fb215 )
by Sébastien
02:43
created

VideoThumbParams   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 98.18%

Importance

Changes 0
Metric Value
wmc 21
eloc 40
dl 0
loc 183
ccs 54
cts 55
cp 0.9818
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withSeekTime() 0 4 1
A withVideoFilter() 0 4 1
A withQualityScale() 0 4 1
A withTime() 0 4 1
A withNoOverwrite() 0 4 1
A withoutParam() 0 8 2
A toArray() 0 3 1
A withFrame() 0 4 1
A withOutputFormat() 0 4 1
A ensureSupportedParams() 0 6 3
A withBuiltInParam() 0 4 1
A getParam() 0 14 3
A isParamValid() 0 3 1
A withOverwrite() 0 4 1
A hasParam() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video;
6
7
use Soluble\MediaTools\Video\Adapter\FFMpegCLIValueInterface;
8
use Soluble\MediaTools\Video\Exception\InvalidArgumentException;
9
use Soluble\MediaTools\Video\Exception\UnsetParamException;
10
use Soluble\MediaTools\Video\Filter\Type\VideoFilterInterface;
11
12
class VideoThumbParams implements VideoThumbParamsInterface
13
{
14
    /** @var array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> */
15
    protected $params = [];
16
17
    /**
18
     * @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params
19
     *
20
     * @throws InvalidArgumentException in case of unsupported option
21
     */
22 18
    public function __construct($params = [])
23
    {
24 18
        $this->ensureSupportedParams($params);
25 18
        $this->params = $params;
26 18
    }
27
28
    /**
29
     * @param float $time time in seconds, decimals are milli
30
     *
31
     * @return VideoThumbParams
32
     */
33 5
    public function withTime(float $time): self
34
    {
35 5
        return new self(array_merge($this->params, [
36 5
            self::PARAM_SEEK_TIME => new SeekTime($time),
37
        ]));
38
    }
39
40 5
    public function withSeekTime(SeekTime $seekTime): self
41
    {
42 5
        return new self(array_merge($this->params, [
43 5
            self::PARAM_SEEK_TIME => $seekTime,
44
        ]));
45
    }
46
47 2
    public function withVideoFilter(VideoFilterInterface $videoFilter): self
48
    {
49 2
        return new self(array_merge($this->params, [
50 2
            self::PARAM_VIDEO_FILTER => $videoFilter,
51
        ]));
52
    }
53
54
    /**
55
     * Set the underlying encoder quality scale. (-qscale:v <int>, alias to -q:v <int>).
56
     *
57
     * @param int $qualityScale a number interpreted by the encoder, generally 1-5
58
     */
59 4
    public function withQualityScale(int $qualityScale): self
60
    {
61 4
        return new self(array_merge($this->params, [
62 4
            self::PARAM_QUALITY_SCALE => $qualityScale,
63
        ]));
64
    }
65
66
    /**
67
     * @param int $frame take this frame
68
     */
69 1
    public function withFrame(int $frame): self
70
    {
71 1
        return new self(array_merge($this->params, [
72 1
            self::PARAM_WITH_FRAME => $frame,
73
        ]));
74
    }
75
76
    /**
77
     * Add with overwrite option (default).
78
     *
79
     * @see self::withNoOverwrite()
80
     */
81 1
    public function withOverwrite(): self
82
    {
83 1
        return new self(array_merge($this->params, [
84 1
            self::PARAM_OVERWRITE => true
85
        ]));
86
    }
87
88
    /**
89
     * Add protection against output file overwriting.
90
     *
91
     * @see self::witoOverwrite()
92
     */
93 1
    public function withNoOverwrite(): self
94
    {
95 1
        return new self(array_merge($this->params, [
96 1
            self::PARAM_OVERWRITE => false
97
        ]));
98
    }
99
100 1
    public function withOutputFormat(string $outputFormat): self
101
    {
102 1
        return new self(array_merge($this->params, [
103 1
            self::PARAM_OUTPUT_FORMAT => $outputFormat,
104
        ]));
105
    }
106
107
    /**
108
     * Set a built-in param...
109
     *
110
     * @param bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface $paramValue
111
     *
112
     * @throws InvalidArgumentException in case of unsupported builtin param
113
     *
114
     * @return self (static analysis the trick is to return 'self' instead of interface)
115
     */
116 1
    public function withBuiltInParam(string $paramName, $paramValue): VideoThumbParamsInterface
117
    {
118 1
        return new self(array_merge($this->params, [
119 1
            $paramName => $paramValue,
120
        ]));
121
    }
122
123
    /**
124
     * @return self (For static analysis the trick is to return 'self' instead of interface)
125
     */
126 1
    public function withoutParam(string $paramName): VideoThumbParamsInterface
127
    {
128 1
        $ao = (new \ArrayObject($this->params));
129 1
        if ($ao->offsetExists($paramName)) {
130 1
            $ao->offsetUnset($paramName);
131
        }
132
133 1
        return new self($ao->getArrayCopy());
134
    }
135
136
    /**
137
     * Return the internal array holding params.
138
     *
139
     * @return array<string,bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface>
140
     */
141 3
    public function toArray(): array
142
    {
143 3
        return $this->params;
144
    }
145
146 16
    public function isParamValid(string $paramName): bool
147
    {
148 16
        return in_array($paramName, self::BUILTIN_PARAMS, true);
149
    }
150
151
    /**
152
     * Return a param, throw an exception if the param has not been defined yet or
153
     * use $default if it was set.
154
     *
155
     * @param mixed $default Will return default value instead of throwing exception
156
     *
157
     * @return bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface|null
158
     *
159
     * @throws UnsetParamException
160
     */
161 11
    public function getParam(string $paramName, $default = null)
162
    {
163 11
        if (!$this->hasParam($paramName)) {
164 2
            if ($default !== null) {
165 1
                return $default;
166
            }
167
168 1
            throw new UnsetParamException(sprintf(
169 1
                'Cannot get param \'%s\', it has not been set',
170 1
                $paramName
171
            ));
172
        }
173
174 9
        return $this->params[$paramName];
175
    }
176
177 12
    public function hasParam(string $paramName): bool
178
    {
179 12
        return array_key_exists($paramName, $this->params);
180
    }
181
182
    /**
183
     * Ensure that all params are supported.
184
     *
185
     * @param array<string, bool|string|int|VideoFilterInterface|FFMpegCLIValueInterface> $params
186
     *
187
     * @throws InvalidArgumentException in case of unsupported option
188
     */
189 18
    protected function ensureSupportedParams(array $params): void
190
    {
191 18
        foreach ($params as $paramName => $paramValue) {
192 16
            if (!$this->isParamValid($paramName)) {
193
                throw new InvalidArgumentException(
194 16
                    sprintf('Unsupported param "%s" given.', $paramName)
195
                );
196
            }
197
        }
198 18
    }
199
}
200