getPluginClassResolvers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 *  This file is part of the Micro framework package.
5
 *
6
 *  (c) Stanislau Komar <[email protected]>
7
 *
8
 *  For the full copyright and license information, please view the LICENSE
9
 *  file that was distributed with this source code.
10
 */
11
12
namespace Micro\Framework\Kernel\Configuration\Resolver;
13
14
use Micro\Framework\Kernel\Configuration\ApplicationConfigurationInterface;
15
use Micro\Framework\Kernel\Configuration\PluginConfiguration;
16
use Micro\Framework\Kernel\Configuration\PluginConfigurationInterface;
17
18
class PluginConfigurationClassResolver
19
{
20 3
    public function __construct(
21
        private readonly ApplicationConfigurationInterface $applicationConfiguration
22
    ) {
23 3
    }
24
25 3
    public function resolve(string $pluginClass): PluginConfigurationInterface
26
    {
27 3
        $configClassDefault = PluginConfiguration::class;
28 3
        $configClasses = [];
29 3
        foreach ($this->getPluginClassResolvers() as $resolver) {
30 3
            $configClass = $resolver->resolve($pluginClass);
31 3
            if (!class_exists($configClass)) {
32 3
                continue;
33
            }
34
35 3
            $configClasses[] = $configClass;
36
        }
37
38 3
        if (\count($configClasses) > 1) {
39
            throw new \RuntimeException(sprintf('Too many configuration classes for Application plugin "%s". [%s]', $pluginClass, implode(', ', $configClasses)));
40
        }
41
42
        /** @var class-string<PluginConfigurationInterface> $configClass */
43 3
        $configClass = $configClasses[0] ?? $configClassDefault;
44
45 3
        return new $configClass($this->applicationConfiguration);
46
    }
47
48
    /**
49
     * @return PluginConfigurationClassResolverInterface[]
50
     */
51 3
    protected function getPluginClassResolvers(): array
52
    {
53 3
        return [
54 3
            new PluginNameResolver(),
55 3
            new PluginNameShortResolver(),
56 3
        ];
57
    }
58
}
59