Failed Conditions
Push — master ( 438024...09a02f )
by Sébastien
03:04
created

PresetLocator::loadBuiltInPresets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Preset;
6
7
use Psr\Container\ContainerInterface;
8
use Soluble\MediaTools\Cli\Exception\UnknownPresetException;
9
use Soluble\MediaTools\Preset\MP4\StreamableH264Preset;
10
use Soluble\MediaTools\Preset\Prod\ResolvePreset;
11
use Soluble\MediaTools\Preset\WebM\GoogleVod2019Preset;
12
13
class PresetLocator
14
{
15
    public const BUILTIN_PRESETS = [
16
        ResolvePreset::class,
17
        StreamableH264Preset::class,
18
        GoogleVod2019Preset::class
19
    ];
20
21
    /**
22
     * @var string[]
23
     */
24
    private $paths = [];
25
26
    /**
27
     * @var array<string, PresetInterface|string>
28
     */
29
    private $presets = [];
30
31
    /**
32
     * @var ContainerInterface
33
     */
34
    private $container;
35
36
    public function __construct(ContainerInterface $container, array $paths = [])
37
    {
38
        $this->paths     = array_merge($paths, [__DIR__]);
39
        $this->container = $container;
40
        $this->loadBuiltInPresets();
41
    }
42
43
    protected function loadBuiltInPresets(): void
44
    {
45
        foreach (self::BUILTIN_PRESETS as $preset) {
46
            $this->presets[$preset] = $preset;
47
        }
48
    }
49
50
    public function addPreset(string $name, PresetInterface $preset): void
51
    {
52
        $this->presets[$name] = $preset;
53
    }
54
55
    public function getPreset(string $name): PresetInterface
56
    {
57
        if (!$this->hasPreset($name)) {
58
            throw new UnknownPresetException(sprintf('Preset %s does not exists', $name));
59
        }
60
        $preset = $this->presets[$name];
61
        if ($preset instanceof PresetInterface) {
62
            return $preset;
63
        }
64
65
        return $this->container->get($name);
66
    }
67
68
    public function hasPreset(string $name): bool
69
    {
70
        return in_array($name, $this->presets, true);
71
    }
72
}
73