Passed
Push — master ( d0bcc1...2a69c8 )
by Sébastien
02:28
created

FFMpegParamValidator::ensureValidCrf()   B

Complexity

Conditions 8
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 8.0291

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
ccs 12
cts 13
cp 0.9231
rs 8.4444
c 0
b 0
f 0
cc 8
nc 4
nop 0
crap 8.0291
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Adapter\Validator;
6
7
use Soluble\MediaTools\Video\Exception\ParamValidationException;
8
use Soluble\MediaTools\Video\VideoConvertParamsInterface;
9
10
/**
11
 * To get better error reporting, we try to fail early (instead of at the shell exec level)
12
 * Here's a starter class to add some common validation, use it as a base, will need
13
 * refactor when more rules are added.
14
 */
15
class FFMpegParamValidator
16
{
17
    /** @var VideoConvertParamsInterface */
18
    protected $params;
19
20 37
    public function __construct(VideoConvertParamsInterface $conversionParams)
21
    {
22 37
        $this->params = $conversionParams;
23 37
    }
24
25
    /**
26
     * @throws ParamValidationException
27
     */
28 37
    public function validate(): void
29
    {
30 37
        $this->ensureValidCrf();
31 36
    }
32
33
    /**
34
     * Ensure that is CRF have been set, values for VP9 and H264 are in valid ranges.
35
     *
36
     * @throws ParamValidationException
37
     */
38 37
    protected function ensureValidCrf(): void
39
    {
40 37
        $crf = $this->params->getParam(VideoConvertParamsInterface::PARAM_CRF, 0);
41
42 37
        if ($crf === null) {
43
            return;
44
        }
45
46
        // Check allowed values for CRF
47 37
        $codec = $this->params->getParam(VideoConvertParamsInterface::PARAM_VIDEO_CODEC, '');
48
49 37
        if (mb_stripos($codec, 'vp9') !== false && ($crf < 0 || $crf > 63)) {
50 1
            throw new ParamValidationException(
51 1
                sprintf(
52 1
                    'Invalid value for CRF, \'%s\' requires a number between 0 and 63: %s given.',
53
                    $codec,
54
                    $crf
55
                )
56
            );
57
        }
58
59 37
        if (mb_stripos($codec, '264') !== false && ($crf < 0 || $crf > 51)) {
60 1
            throw new ParamValidationException(
61 1
                sprintf(
62 1
                    'Invalid value for CRF, \'%s\' requires a number between 0 and 61: %s given.',
63
                    $codec,
64
                    $crf
65
                )
66
            );
67
        }
68 36
    }
69
}
70