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

ALoader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 20%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 35
ccs 3
cts 15
cp 0.2
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A load() 0 17 5
1
<?php
2
3
namespace kalanis\kw_modules\Loaders\KwDi;
4
5
6
use kalanis\kw_autoload\DependencyInjection;
0 ignored issues
show
Bug introduced by
The type kalanis\kw_autoload\DependencyInjection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use kalanis\kw_modules\Interfaces\ILoader;
8
use kalanis\kw_modules\Interfaces\IMdTranslations;
9
use kalanis\kw_modules\Interfaces\IModule;
10
use kalanis\kw_modules\ModuleException;
11
use kalanis\kw_modules\Traits\TMdLang;
12
use ReflectionException;
13
14
15
/**
16
 * Class ALoader
17
 * @package kalanis\kw_modules\Loaders\KwDi
18
 * Load modules data from defined targets
19
 * @codeCoverageIgnore contains external autoloader
20
 *
21
 * Load with DI from kw_autoloader
22
 */
23
abstract class ALoader implements ILoader
24
{
25
    use TMdLang;
26
27 1
    public function __construct(?IMdTranslations $lang = null)
28
    {
29 1
        $this->setMdLang($lang);
30 1
    }
31
32
    public function load(array $module, array $constructParams = []): ?IModule
33
    {
34
        $classPath = $this->getClassName($module);
35
        try {
36
            $di = DependencyInjection::getInstance();
37
            if (!$module = $di->getRep($classPath)) {
38
                $module = $di->initClass($classPath, $constructParams);
39
            }
40
            if (empty($module)) {
41
                return null;
42
            }
43
            if (!$module instanceof IModule) {
44
                throw new ModuleException($this->getMdLang()->mdNotInstanceOfIModule($classPath));
45
            }
46
            return $module;
47
        } catch (ReflectionException $ex) {
48
            return null;
49
        }
50
    }
51
52
    /**
53
     * @param string[] $module
54
     * @throws ModuleException
55
     * @return string
56
     */
57
    abstract protected function getClassName(array $module): string;
58
}
59