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

ALoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 32
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
    /** @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