Completed
Push — master ( 070a7f...5725b1 )
by Sébastien
04:16
created

FFMpegConfigFactory::__invoke()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.1054

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
ccs 13
cts 16
cp 0.8125
rs 9.7333
c 0
b 0
f 0
cc 4
nc 5
nop 1
crap 4.1054
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Config;
6
7
use Psr\Container\ContainerExceptionInterface;
8
use Psr\Container\ContainerInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use Soluble\MediaTools\Exception\InvalidConfigException;
11
use Soluble\MediaTools\Util\SafeConfigReader;
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 16
    public function __construct(
25
        string $entryName = self::DEFAULT_ENTRY_NAME,
26
        ?string $configKey = self::DEFAULT_CONFIG_KEY
27
    ) {
28 16
        $this->entryName = $entryName;
29 16
        $this->configKey = $configKey;
30 16
    }
31
32
    /**
33
     * @throws InvalidConfigException
34
     */
35 16
    public function __invoke(ContainerInterface $container): FFMpegConfig
36
    {
37
        try {
38 16
            $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 16
        $config = $this->configKey === null ? $containerConfig : ($containerConfig[$this->configKey] ?? null);
46
47 16
        if (!is_array($config)) {
48 1
            throw new InvalidConfigException(
49 1
                sprintf('Cannot find a configuration ($entryName=%s found, invalid $configKey=%s).', $this->entryName, $this->configKey)
50
            );
51
        }
52
53 15
        $scr = new SafeConfigReader($config, $this->configKey ?? '');
54
55 15
        return new FFMpegConfig(
56 15
            $scr->getString('ffmpeg.binary', FFMpegConfig::DEFAULT_BINARY),
57 14
            $scr->getNullableInt('ffmpeg.threads', FFMpegConfig::DEFAULT_THREADS),
58 14
            $scr->getNullableInt('ffmpeg.timeout', FFMpegConfig::DEFAULT_TIMEOUT),
59 14
            $scr->getNullableInt('ffmpeg.idle_timeout', FFMpegConfig::DEFAULT_IDLE_TIMEOUT),
60 14
            $scr->getArray('ffmpeg.env', FFMpegConfig::DEFAULT_ENV)
61
        );
62
    }
63
}
64