PluginClassResolver::resolve()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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\Plugin\Configuration\Helper\Business\Plugin;
13
14
use Micro\Framework\Kernel\Configuration\Exception\InvalidConfigurationException;
15
use Micro\Framework\Kernel\KernelInterface;
16
17
class PluginClassResolver implements PluginClassResolverInterface
18
{
19 1
    public function __construct(private readonly KernelInterface $kernel)
20
    {
21 1
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26 1
    public function resolve(string $pluginAlias): object
27
    {
28 1
        foreach ($this->kernel->plugins() as $plugin) {
29 1
            if ($this->getPluginName($plugin) === $pluginAlias) {
30 1
                return $plugin;
31
            }
32
        }
33
34 1
        throw new InvalidConfigurationException(sprintf('Plugin %s is not installed.', $pluginAlias));
35
    }
36
37 1
    protected function getPluginName(object $plugin): string
38
    {
39
        // TODO: Create interface for plugin naming
40 1
        if (method_exists($plugin, 'name')) {
41 1
            return $plugin->name();
42
        }
43
44 1
        $pluginName = \get_class($plugin);
45 1
        if (class_exists($pluginName)) {
46 1
            $exploded = explode('\\', $pluginName);
47
48 1
            return array_pop($exploded);
49
        }
50
51
        return $pluginName;
52
    }
53
}
54