Passed
Push — master ( d95e49...7c87fb )
by Andrés
39s
created

TaskFactory::loadCustomTasks()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
ccs 13
cts 13
cp 1
rs 8.7624
cc 5
eloc 11
nc 5
nop 1
crap 5
1
<?php
2
/*
3
 * This file is part of the Magallanes package.
4
 *
5
 * (c) Andrés Montañez <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Mage\Task;
12
13
use Mage\Runtime\Runtime;
14
use Mage\Runtime\Exception\RuntimeException;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
use ReflectionClass;
18
19
/**
20
 * Task Factory
21
 *
22
 * @author Andrés Montañez <[email protected]>
23
 */
24
class TaskFactory
25
{
26
    /**
27
     * @var Runtime
28
     */
29
    protected $runtime;
30
31
    /**
32
     * @var array Registered Tasks
33
     */
34
    protected $registeredTasks = [];
35
36
    /**
37
     * Constructor
38
     *
39
     * @param Runtime $runtime
40
     */
41 38
    public function __construct(Runtime $runtime)
42
    {
43 38
        $this->runtime = $runtime;
44 38
        $this->loadBuiltInTasks();
45 38
        $this->loadCustomTasks($runtime->getConfigOption('custom_tasks', []));
46 35
    }
47
48
    /**
49
     * Add a Task
50
     *
51
     * @param AbstractTask $task
52
     */
53 38
    public function add(AbstractTask $task)
54
    {
55 38
        $task->setRuntime($this->runtime);
56 38
        $this->registeredTasks[$task->getName()] = $task;
57 38
    }
58
59
    /**
60
     * Get a Task by it's registered Name/Code, or it can be a Class Name,
61
     * in that case the class will be instantiated
62
     *
63
     * @param string $name Name/Code or Class of the Task
64
     * @return AbstractTask
65
     * @throws RuntimeException
66
     */
67 33
    public function get($name)
68
    {
69 33
        $options = [];
70 33
        if (is_array($name)) {
71 14
            $options = $name;
72 14
            list($name) = array_keys($name);
73 14
            $options = $options[$name];
74 14
        }
75
76 33
        if (array_key_exists($name, $this->registeredTasks)) {
77
            /** @var AbstractTask $task */
78 30
            $task = $this->registeredTasks[$name];
79 30
            $task->setOptions($options);
80 30
            return $task;
81 4
        } elseif (class_exists($name)) {
82 2
            $reflex = new ReflectionClass($name);
83 2
            if ($reflex->isInstantiable()) {
84 2
                $task = new $name();
85 2
                if ($task instanceof AbstractTask) {
86 1
                    $task->setOptions($options);
87 1
                    $this->add($task);
88 1
                    return $task;
89
                }
90 1
            }
91 1
        }
92
93 3
        throw new RuntimeException(sprintf('Invalid task name "%s"', $name));
94
    }
95
96
    /**
97
     * Load BuiltIn Tasks
98
     */
99 38 View Code Duplication
    protected function loadBuiltInTasks()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101 38
        $finder = new Finder();
102 38
        $finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php');
103
104
        /** @var SplFileInfo $file */
105 38
        foreach ($finder as $file) {
106 38
            $taskClass = substr('\\Mage\\Task\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
107 38
            if (class_exists($taskClass)) {
108 38
                $reflex = new ReflectionClass($taskClass);
109 38
                if ($reflex->isInstantiable()) {
110 38
                    $task = new $taskClass();
111 38
                    if ($task instanceof AbstractTask) {
112 38
                        $this->add($task);
113 38
                    }
114 38
                }
115 38
            }
116 38
        }
117 38
    }
118
119
    /**
120
     * Load Custom Tasks
121
     * @param array $tasksToLoad PreRegistered Tasks
122
     * @throws RuntimeException
123
     */
124 38
    protected function loadCustomTasks($tasksToLoad)
125
    {
126 38
        foreach ($tasksToLoad as $taskClass) {
127 4
            if (!class_exists($taskClass)) {
128 1
                throw new RuntimeException(sprintf('Custom Task "%s" does not exists.', $taskClass));
129
            }
130
131 3
            $reflex = new ReflectionClass($taskClass);
132 3
            if (!$reflex->isInstantiable()) {
133 1
                throw new RuntimeException(sprintf('Custom Task "%s" can not be instantiated.', $taskClass));
134
            }
135
136 2
            $task = new $taskClass();
137 2
            if (!$task instanceof AbstractTask) {
138 1
                throw new RuntimeException(sprintf('Custom Task "%s" must inherit "Mage\\Task\\AbstractTask".', $taskClass));
139
            }
140
141
            // Add Task
142 1
            $this->add($task);
143 35
        }
144 35
    }
145
}
146