TaskFactory::get()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 18
c 0
b 0
f 0
nc 10
nop 1
dl 0
loc 27
ccs 19
cts 19
cp 1
crap 6
rs 9.0444
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Task;
13
14
use Mage\Runtime\Runtime;
15
use Mage\Runtime\Exception\RuntimeException;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Finder\SplFileInfo;
18
use ReflectionClass;
19
20
/**
21
 * Task Factory
22
 *
23
 * @author Andrés Montañez <[email protected]>
24
 */
25
class TaskFactory
26
{
27
    protected Runtime $runtime;
28
29
    /**
30
     * @var AbstractTask[] Registered Tasks
31
     */
32
    protected array $registeredTasks = [];
33
34
    /**
35
     * Constructor
36
     */
37 48
    public function __construct(Runtime $runtime)
38
    {
39 48
        $this->runtime = $runtime;
40 48
        $this->loadBuiltInTasks();
41 48
        $this->loadCustomTasks($runtime->getConfigOption('custom_tasks', []));
42
    }
43
44
    /**
45
     * Add a Task
46
     */
47 48
    public function add(AbstractTask $task): void
48
    {
49 48
        $task->setRuntime($this->runtime);
50 48
        $this->registeredTasks[$task->getName()] = $task;
51
    }
52
53
    /**
54
     * Get a Task by it's registered Name/Code, or it can be a Class Name,
55
     * in that case the class will be instantiated
56
     *
57
     * @param string|mixed[] $name
58
     * @throws RuntimeException
59
     */
60 42
    public function get(mixed $name): AbstractTask
61
    {
62 42
        $options = [];
63 42
        if (is_array($name)) {
64 23
            $options = $name;
65 23
            list($name) = array_keys($name);
66 23
            $options = $options[$name];
67
        }
68
69 42
        if (array_key_exists($name, $this->registeredTasks)) {
70
            /** @var AbstractTask $task */
71 39
            $task = $this->registeredTasks[$name];
72 39
            $task->setOptions($options);
73 39
            return $task;
74 4
        } elseif (class_exists($name)) {
75 2
            $reflex = new ReflectionClass($name);
76 2
            if ($reflex->isInstantiable()) {
77 2
                $task = new $name();
78 2
                if ($task instanceof AbstractTask) {
79 1
                    $task->setOptions($options);
80 1
                    $this->add($task);
81 1
                    return $task;
82
                }
83
            }
84
        }
85
86 3
        throw new RuntimeException(sprintf('Invalid task name "%s"', $name));
87
    }
88
89
    /**
90
     * Load BuiltIn Tasks
91
     */
92 48
    protected function loadBuiltInTasks(): void
93
    {
94 48
        $finder = new Finder();
95 48
        $finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php');
96
97
        /** @var SplFileInfo $file */
98 48
        foreach ($finder as $file) {
99 48
            $taskClass = substr(
100 48
                '\\Mage\\Task\\BuiltIn\\' . str_replace(
101
                    '/',
102
                    '\\',
103 48
                    $file->getRelativePathname()
104
                ),
105
                0,
106
                -4
107
            );
108 48
            if (class_exists($taskClass)) {
109 48
                $reflex = new ReflectionClass($taskClass);
110 48
                if ($reflex->isInstantiable()) {
111 48
                    $task = new $taskClass();
112 48
                    if ($task instanceof AbstractTask) {
113 48
                        $this->add($task);
114
                    }
115
                }
116
            }
117
        }
118
    }
119
120
    /**
121
     * Load Custom Tasks
122
     *
123
     * @param string[] $tasksToLoad
124
     * @throws RuntimeException
125
     */
126 48
    protected function loadCustomTasks(array $tasksToLoad): void
127
    {
128 48
        foreach ($tasksToLoad as $taskClass) {
129 4
            if (!class_exists($taskClass)) {
130 1
                throw new RuntimeException(sprintf('Custom Task "%s" does not exists.', $taskClass));
131
            }
132
133 3
            $reflex = new ReflectionClass($taskClass);
134 3
            if (!$reflex->isInstantiable()) {
135 1
                throw new RuntimeException(sprintf('Custom Task "%s" can not be instantiated.', $taskClass));
136
            }
137
138 2
            $task = new $taskClass();
139 2
            if (!$task instanceof AbstractTask) {
140 1
                throw new RuntimeException(
141 1
                    sprintf('Custom Task "%s" must inherit "Mage\\Task\\AbstractTask".', $taskClass)
142
                );
143
            }
144
145
            // Add Task
146 1
            $this->add($task);
147
        }
148
    }
149
}
150