Passed
Push — master ( c03b7d...d95c98 )
by Sébastien
01:47
created

PresetLocator::loadBuiltInPresets()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
    /** @var string[] */
22
    private $paths = [];
23
24
    /** @var array<string, PresetInterface|string> */
25
    private $presets = [];
26
27
    /** @var ContainerInterface */
28
    private $container;
29
30 3
    public function __construct(ContainerInterface $container, array $paths = [])
31
    {
32 3
        $this->paths     = array_merge($paths, [__DIR__]);
33 3
        $this->container = $container;
34 3
        $this->loadBuiltInPresets();
35 3
    }
36
37 3
    protected function loadBuiltInPresets(): void
38
    {
39 3
        foreach (self::BUILTIN_PRESETS as $preset) {
40 3
            $this->presets[$preset] = $preset;
41
        }
42 3
    }
43
44 3
    public function addPreset(string $name, PresetInterface $preset): void
45
    {
46 3
        $this->presets[$name] = $preset;
47 3
    }
48
49 1
    public function getPreset(string $name): PresetInterface
50
    {
51 1
        if (!$this->hasPreset($name)) {
52
            throw new UnknownPresetException(sprintf('Preset %s does not exists', $name));
53
        }
54 1
        $preset = $this->presets[$name];
55 1
        if ($preset instanceof PresetInterface) {
56 1
            return $preset;
57
        }
58
59
        return $this->container->get($name);
60
    }
61
62 1
    public function hasPreset(string $name): bool
63
    {
64 1
        return array_key_exists($name, $this->presets);
65
    }
66
}
67