KwDiLoader::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
namespace kalanis\kw_clipr\Loaders;
4
5
6
use kalanis\kw_autoload\DependencyInjection;
7
use kalanis\kw_clipr\Clipr\Useful;
8
use kalanis\kw_clipr\CliprException;
9
use kalanis\kw_clipr\Interfaces;
10
use kalanis\kw_clipr\Tasks\ATask;
11
use ReflectionException;
12
13
14
/**
15
 * Class KwDiLoader
16
 * @package kalanis\kw_clipr\Tasks
17
 * Factory for creating tasks/commands from obtained name
18
 * It contains dependency injection - so everything loaded is from source targeted by that DI
19
 * @codeCoverageIgnore because of that internal autoloader
20
 */
21
class KwDiLoader implements Interfaces\ITargetDirs
22
{
23
    /** @var array<string, array<string>> */
24
    protected array $paths = [];
25
    /** @var array<mixed> */
26
    protected array $additionalParams = [];
27
28
    /**
29
     * @param array<string, array<string>> $paths where will DI be looking for tasks
30
     * @param array<mixed> $additionalParams
31
     * @throws CliprException
32
     */
33
    public function __construct(array $paths = [], array $additionalParams = [])
34
    {
35
        foreach ($paths as $namespace => $path) {
36
            $pt = implode(DIRECTORY_SEPARATOR, $path);
37
            if (false === $real = realpath($pt)) {
38
                throw new CliprException(sprintf('Unknown path *%s*!', $pt), Interfaces\IStatuses::STATUS_BAD_CONFIG);
39
            }
40
            $this->paths[$namespace] = explode(DIRECTORY_SEPARATOR, $real);
41
        }
42
        $this->additionalParams = $additionalParams;
43
    }
44
45
    /**
46
     * @param string $classFromParam
47
     * @throws CliprException
48
     * @throws ReflectionException
49
     * @return ATask|null
50
     */
51
    public function getTask(string $classFromParam): ?ATask
52
    {
53
        $classPath = Useful::sanitizeClass($classFromParam);
54
        $di = DependencyInjection::getInstance();
55
        foreach ($this->paths as $namespace => $path) {
56
            if ($this->containsPath($classPath, $namespace)) {
57
                $task = $di->initClass($classPath, $this->additionalParams);
58
                if (!$task instanceof ATask) {
59
                    // the class inside is not an instance of ATask necessary to run
60
                    throw new CliprException(sprintf('Class *%s* is not instance of ATask - check interface or query', $classPath), Interfaces\IStatuses::STATUS_LIB_ERROR);
61
                }
62
                return $task;
63
            }
64
        }
65
        return null;
66
    }
67
68
    protected function containsPath(string $classPath, string $namespace): bool
69
    {
70
        return (0 === mb_strpos($classPath, $namespace));
71
    }
72
73
    public function getPaths(): array
74
    {
75
        return $this->paths;
76
    }
77
}
78