Passed
Push — master ( af560c...690258 )
by Sébastien
03:19
created

FFMpegConfigFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 26 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Video\Config;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use Soluble\MediaTools\Common\Config\SafeConfigReader;
11
use Soluble\MediaTools\Common\Exception\InvalidConfigException;
12
13
class FFMpegConfigFactory
14
{
15
    public const DEFAULT_ENTRY_NAME = 'config';
16
    public const DEFAULT_CONFIG_KEY = 'soluble-mediatools';
17
18
    /** @var string */
19
    protected $entryName;
20
21
    /** @var null|string */
22
    protected $configKey;
23
24
    public function __construct(
25
        string $entryName = self::DEFAULT_ENTRY_NAME,
26
        ?string $configKey = self::DEFAULT_CONFIG_KEY
27
    ) {
28
        $this->entryName = $entryName;
29
        $this->configKey = $configKey;
30
    }
31
32
    /**
33
     * @throws InvalidConfigException
34
     */
35
    public function __invoke(ContainerInterface $container): FFMpegConfig
36
    {
37
        try {
38
            $containerConfig = $container->get($this->entryName);
39
        } catch (NotFoundExceptionInterface | ContainerExceptionInterface $e) {
40
            throw new InvalidConfigException(
41
                sprintf('Cannot resolve container entry \'%s\' ($entryName).', $this->entryName)
42
            );
43
        }
44
45
        $config = $this->configKey === null ? $containerConfig : ($containerConfig[$this->configKey] ?? null);
46
47
        if (!is_array($config)) {
48
            throw new InvalidConfigException(
49
                sprintf('Cannot find a configuration ($entryName=%s found, invalid $configKey=%s).', $this->entryName, $this->configKey)
50
            );
51
        }
52
53
        $scr = new SafeConfigReader($config, $this->configKey ?? '');
54
55
        return new FFMpegConfig(
56
            $scr->getString('ffmpeg.binary', FFMpegConfig::DEFAULT_BINARY),
57
            $scr->getNullableInt('ffmpeg.threads', FFMpegConfig::DEFAULT_THREADS),
58
            $scr->getNullableFloat('ffmpeg.timeout', FFMpegConfig::DEFAULT_TIMEOUT),
59
            $scr->getNullableFloat('ffmpeg.idle_timeout', FFMpegConfig::DEFAULT_IDLE_TIMEOUT),
60
            $scr->getArray('ffmpeg.env', FFMpegConfig::DEFAULT_ENV)
61
        );
62
    }
63
}
64