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

ALoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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