Completed
Push — master ( 0ab6a2...d8ddb3 )
by Robbie
01:17
created

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 94.44%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 58
ccs 17
cts 18
cp 0.9444
rs 10
c 2
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommandFromTask() 0 15 3
A getCommandName() 0 10 2
A getTaskAsClosure() 0 11 1
1
<?php
2
3
namespace SilverLeague\Console\Command;
4
5
use SilverLeague\Console\Command\Dev\Tasks\AbstractTaskCommand;
6
use SilverLeague\Console\Framework\ConsoleBase;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Dev\BuildTask;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
/**
15
 * The Command Factory class is reponsible for converting a SilverStripe BuildTask into a Symfony Console Command
16
 *
17
 * @package silverstripe-console
18
 * @author  Robbie Averill <[email protected]>
19
 */
20
class Factory extends ConsoleBase
21
{
22
    /**
23
     * Given a BuildTask, convert it to a runnable Symfony Command
24
     *
25
     * @param  BuildTask $task
26
     * @return SilverStripeCommand
27
     */
28 2
    public function getCommandFromTask(BuildTask $task)
29
    {
30 2
        if (!is_callable([$task, 'run']) || !$task->isEnabled()) {
31
            return false;
32
        }
33
34 2
        $command = new AbstractTaskCommand($this->getCommandName($task));
35 2
        $command->setApplication($this->getApplication());
36 2
        $command->setTask($task);
37 2
        $command->setDescription($task->getTitle());
38 2
        $command->setHelp($task->getDescription());
39 2
        $command->setCode($this->getTaskAsClosure($command));
40
41 2
        return $command;
42
    }
43
44
    /**
45
     * Get a Command name from the BuildTask segment
46
     *
47
     * @return string
48
     */
49 2
    public function getCommandName(BuildTask $task)
50
    {
51 2
        $taskSegment = Config::inst()->get(get_class($task), 'segment');
52 2
        $segment = strtolower(preg_replace('/(?<=[a-z])([A-Z]+)/', '-$1', $taskSegment));
53
        // We don't really need "-task" on the end of every task.
54 2
        if (substr($segment, -5, 5) === '-task') {
55 2
            $segment = substr($segment, 0, strlen($segment) - 5);
56
        }
57 2
        return sprintf('dev:tasks:%s', $segment);
58
    }
59
60
    /**
61
     * Get the BuildTask functionality as a closure
62
     *
63
     * @param  AbstractTaskCommand $command
64
     * @return Closure
65
     */
66
    public function getTaskAsClosure(AbstractTaskCommand $command)
67
    {
68 2
        return function (InputInterface $input, OutputInterface $output) use ($command) {
69
            $io = new SymfonyStyle($input, $output);
70
71
            $io->title(sprintf('%s: %s', $command->getName(), $command->getDescription()));
72
            $io->section('Running...');
73
74
            $command->getTask()->run(new HTTPRequest('GET', '/'));
75 2
        };
76
    }
77
}
78