PluginConfigurationClassResolver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 15
c 2
b 1
f 0
dl 0
loc 38
ccs 17
cts 18
cp 0.9444
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPluginClassResolvers() 0 5 1
A __construct() 0 3 1
A resolve() 0 21 4
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