ALoader::load()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 12
rs 10
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