Passed
Push — master ( 53c725...a8eb1b )
by Petr
07:45
created

ALoader::load()   A

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
    /** @var ContainerInterface */
28
    protected $container = null;
29
30
    public function __construct(ContainerInterface $container, ?IMdTranslations $lang = null)
31
    {
32
        $this->setMdLang($lang);
33
        $this->container = $container;
34
    }
35
36
    public function load(array $module, array $constructParams = []): ?IModule
37
    {
38
        $classPath = $this->getClassName($module);
39
        if ($this->container->has($classPath)) {
40
            $module = $this->container->get($classPath);
41
            if (!$module instanceof IModule) {
42
                throw new ModuleException($this->getMdLang()->mdNotInstanceOfIModule($classPath));
43
            }
44
            return $module;
45
        }
46
        return null;
47
    }
48
49
    /**
50
     * @param string[] $module
51
     * @throws ModuleException
52
     * @return string
53
     */
54
    abstract protected function getClassName(array $module): string;
55
}
56