Passed
Push — master ( ba3f64...7d7d37 )
by Robbie
02:10
created

SilverStripeLoader::taskIsAbstract()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace SilverLeague\Console\Framework\Loader;
4
5
use ReflectionClass;
6
use SilverLeague\Console\Command\Factory;
7
use SilverLeague\Console\Framework\ConsoleBase;
8
use SilverStripe\Core\ClassInfo;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Dev\BuildTask;
11
12
/**
13
 * The class responsible for loading/instantiating SilverStripe and accessing its class hierarchy, etc
14
 *
15
 * @package silverstripe-console
16
 * @author  Robbie Averill <[email protected]>
17
 */
18
class SilverStripeLoader extends ConsoleBase
19
{
20
    /**
21
     * Return a set of Tasks from SilverStripe
22
     *
23 3
     * @return array
24
     */
25 3
    public function getTasks()
26 3
    {
27
        $commands = [];
28
        $tasks = ClassInfo::subclassesFor(BuildTask::class);
29 3
30
        // Remove the BuildTask itself
31 3
        array_shift($tasks);
32 3
33 3
        foreach ($tasks as $taskClass) {
34
            if ($this->taskIsAbstract($taskClass)) {
35
                continue;
36 3
            }
37
38
            $task = Injector::inst()->get($taskClass);
39
            if ($command = $this->getCommandFactory()->getCommandFromTask($task)) {
40
                $commands[] = $command;
41
            }
42
        }
43
44 4
        return $commands;
45
    }
46 4
47
    /**
48
     * Get a new command factory instance to generate the console Command
49
     *
50
     * @return Factory
51
     */
52
    public function getCommandFactory()
53
    {
54
        return new Factory($this->getApplication());
55
    }
56
57
    /**
58
     * Check whether the given task class is abstract
59
     *
60
     * @param string $className
61
     * @return bool
62
     */
63
    protected function taskIsAbstract($className)
64
    {
65
        $reflection = new ReflectionClass($className);
66
        return $reflection->isAbstract();
67
    }
68
}
69