ALoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 31
ccs 0
cts 23
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 11 3
1
<?php
2
3
namespace kalanis\kw_modules\Loaders\Di;
4
5
6
use kalanis\kw_modules\Interfaces\ILoader;
7
use kalanis\kw_modules\Interfaces\IMdTranslations;
8
use kalanis\kw_modules\Interfaces\IModule;
9
use kalanis\kw_modules\ModuleException;
10
use kalanis\kw_modules\Traits\TMdLang;
11
use Psr\Container\ContainerInterface;
12
13
14
/**
15
 * Class ALoader
16
 * @package kalanis\kw_modules\Loaders\Di
17
 * Use Dependency Injection
18
 * @codeCoverageIgnore contains external autoloader
19
 *
20
 * Name is passed as first big and the rest little ( ucfirst(strtolower($x)) )
21
 * - lookup by curly braces
22
 */
23
abstract class ALoader implements ILoader
24
{
25
    use TMdLang;
26
27
    protected ContainerInterface $container;
28
29
    public function __construct(ContainerInterface $container, ?IMdTranslations $lang = null)
30
    {
31
        $this->setMdLang($lang);
32
        $this->container = $container;
33
    }
34
35
    public function load(array $module, array $constructParams = []): ?IModule
36
    {
37
        $classPath = $this->getClassName($module);
38
        if ($this->container->has($classPath)) {
39
            $module = $this->container->get($classPath);
40
            if (!$module instanceof IModule) {
41
                throw new ModuleException($this->getMdLang()->mdNotInstanceOfIModule($classPath));
42
            }
43
            return $module;
44
        }
45
        return null;
46
    }
47
48
    /**
49
     * @param string[] $module
50
     * @throws ModuleException
51
     * @return string
52
     */
53
    abstract protected function getClassName(array $module): string;
54
}
55