Test Failed
Pull Request — master (#448)
by
unknown
05:18
created

TaskFactory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 122
Duplicated Lines 15.57 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 19
loc 122
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadCustomTasks() 0 21 5
A __construct() 0 6 1
A add() 0 5 1
B get() 0 28 6
A loadBuiltInTasks() 19 19 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct(Runtime $runtime)
42
    {
43
        $this->runtime = $runtime;
44
        $this->loadBuiltInTasks();
45
        $this->loadCustomTasks($runtime->getConfigOption('custom_tasks', []));
46
    }
47
48
    /**
49
     * Add a Task
50
     *
51
     * @param AbstractTask $task
52
     */
53
    public function add(AbstractTask $task)
54
    {
55
        $task->setRuntime($this->runtime);
56
        $this->registeredTasks[$task->getName()] = $task;
57
    }
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
    public function get($name)
68
    {
69
        $options = [];
70
        if (is_array($name)) {
71
            $options = $name;
72
            list($name) = array_keys($name);
73
            $options = $options[$name];
74
        }
75
76
        if (array_key_exists($name, $this->registeredTasks)) {
77
            /** @var AbstractTask $task */
78
            $task = $this->registeredTasks[$name];
79
            $task->setOptions($options);
80
            return $task;
81
        } elseif (class_exists($name)) {
82
            $reflex = new ReflectionClass($name);
83
            if ($reflex->isInstantiable()) {
84
                $task = new $name();
85
                if ($task instanceof AbstractTask) {
86
                    $task->setOptions($options);
87
                    $this->add($task);
88
                    return $task;
89
                }
90
            }
91
        }
92
93
        throw new RuntimeException(sprintf('Invalid task name "%s"', $name));
94
    }
95
96
    /**
97
     * Load BuiltIn Tasks
98
     */
99 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
        $finder = new Finder();
102
        $finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php');
103
104
        /** @var SplFileInfo $file */
105
        foreach ($finder as $file) {
106
            $taskClass = substr('\\Mage\\Task\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
107
            if (class_exists($taskClass)) {
108
                $reflex = new ReflectionClass($taskClass);
109
                if ($reflex->isInstantiable()) {
110
                    $task = new $taskClass();
111
                    if ($task instanceof AbstractTask) {
112
                        $this->add($task);
113
                    }
114
                }
115
            }
116
        }
117
    }
118
119
    /**
120
     * Load Custom Tasks
121
     * @param array $tasksToLoad PreRegistered Tasks
122
     * @throws RuntimeException
123
     */
124
    protected function loadCustomTasks($tasksToLoad)
125
    {
126
        foreach ($tasksToLoad as $taskClass) {
127
            if (!class_exists($taskClass)) {
128
                throw new RuntimeException(sprintf('Custom Task "%s" does not exists.', $taskClass));
129
            }
130
131
            $reflex = new ReflectionClass($taskClass);
132
            if (!$reflex->isInstantiable()) {
133
                throw new RuntimeException(sprintf('Custom Task "%s" can not be instantiated.', $taskClass));
134
            }
135
136
            $task = new $taskClass();
137
            if (!$task instanceof AbstractTask) {
138
                throw new RuntimeException(sprintf('Custom Task "%s" must inherit "Mage\\Task\\AbstractTask".', $taskClass));
139
            }
140
141
            // Add Task
142
            $this->add($task);
143
        }
144
    }
145
}
146