Failed Conditions
Push — master ( a3d1dc...25914a )
by Sébastien
02:18
created

FFMpegParamValidator::__construct()   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 1
crap 1
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
    /**
18
     * @var VideoConvertParamsInterface
19
     */
20
    protected $params;
21
22 29
    public function __construct(VideoConvertParamsInterface $conversionParams)
23
    {
24 29
        $this->params = $conversionParams;
25 29
    }
26
27
    /**
28
     * @throws ParamValidationException
29
     */
30 29
    public function validate(): void
31
    {
32 29
        $this->ensureValidCrf();
33 28
    }
34
35
    /**
36
     * Ensure that is CRF have been set, values for VP9 and H264 are in valid ranges.
37
     *
38
     * @throws ParamValidationException
39
     */
40 29
    protected function ensureValidCrf(): void
41
    {
42 29
        $crf = $this->params->getParam(VideoConvertParamsInterface::PARAM_CRF, 0);
43
44 29
        if ($crf !== null) {
45
            // Check allowed values for CRF
46 29
            $codec = $this->params->getParam(VideoConvertParamsInterface::PARAM_VIDEO_CODEC, '');
47
48 29
            if (false !== mb_stripos($codec, 'vp9') && ($crf < 0 || $crf > 63)) {
49 1
                throw new ParamValidationException(
50 1
                    sprintf(
51 1
                        'Invalid value for CRF, \'%s\' requires a number between 0 and 63: %s given.',
52 1
                        $codec,
53 1
                        $crf
54
                    )
55
                );
56 29
            } elseif (false !== mb_stripos($codec, '264') && ($crf < 0 || $crf > 51)) {
57 1
                throw new ParamValidationException(
58 1
                    sprintf(
59 1
                        'Invalid value for CRF, \'%s\' requires a number between 0 and 61: %s given.',
60 1
                        $codec,
61 1
                        $crf
62
                    )
63
                );
64
            }
65
        }
66 28
    }
67
}
68