|
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 FFProbeConfigFactory |
|
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
|
10 |
|
public function __construct( |
|
25
|
|
|
string $entryName = self::DEFAULT_ENTRY_NAME, |
|
26
|
|
|
?string $configKey = self::DEFAULT_CONFIG_KEY |
|
27
|
|
|
) { |
|
28
|
10 |
|
$this->entryName = $entryName; |
|
29
|
10 |
|
$this->configKey = $configKey; |
|
30
|
10 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @throws InvalidConfigException |
|
34
|
|
|
*/ |
|
35
|
10 |
|
public function __invoke(ContainerInterface $container): FFProbeConfig |
|
36
|
|
|
{ |
|
37
|
|
|
try { |
|
38
|
10 |
|
$containerConfig = $container->get($this->entryName); |
|
39
|
1 |
|
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $e) { |
|
40
|
1 |
|
throw new InvalidConfigException( |
|
41
|
1 |
|
sprintf('Cannot resolve container entry \'%s\' ($entryName).', $this->entryName) |
|
42
|
|
|
); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
$config = $this->configKey === null ? $containerConfig : ($containerConfig[$this->configKey] ?? null); |
|
46
|
|
|
|
|
47
|
9 |
|
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
|
8 |
|
$scr = new SafeConfigReader($config, $this->configKey ?? ''); |
|
54
|
|
|
|
|
55
|
8 |
|
return new FFProbeConfig( |
|
56
|
8 |
|
$scr->getString('ffprobe.binary', FFProbeConfig::DEFAULT_BINARY), |
|
57
|
7 |
|
$scr->getNullableInt('ffprobe.timeout', FFProbeConfig::DEFAULT_TIMEOUT), |
|
58
|
7 |
|
$scr->getNullableInt('ffprobe.idle_timeout', FFProbeConfig::DEFAULT_IDLE_TIMEOUT), |
|
59
|
7 |
|
$scr->getArray('ffprobe.env', FFProbeConfig::DEFAULT_ENV) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|