Completed
Pull Request — master (#419)
by Andrés
05:59
created

ExecTask::getDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
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\BuiltIn;
12
13
use Mage\Task\Exception\ErrorException;
14
use Mage\Task\AbstractTask;
15
use Symfony\Component\Process\Process;
16
17
/**
18
 * Exec task. Allows you to execute arbitrary commands.
19
 *
20
 * @author Yanick Witschi <https://github.com/Toflar>
21
 */
22
class ExecTask extends AbstractTask
23
{
24
    /**
25
     * @return string
26
     */
27
    public function getName()
28
    {
29
        return 'exec';
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getDescription()
36
    {
37
        $options = $this->getOptions();
38
39
        if ($options['desc']) {
40
            return '[Exec] ' . $options['desc'];
41
        }
42
43
        return '[Exec] Custom command';
44
    }
45
46
    /**
47
     * @return bool
48
     *
49
     * @throws ErrorException
50
     */
51
    public function execute()
52
    {
53
        $options = $this->getOptions();
54
55
        if (!$options['cmd']) {
56
            throw new ErrorException('Parameter "cmd" is not defined');
57
        }
58
59
        /** @var Process $process */
60
        $process = $this->runtime->runCommand($options['cmd'], $options['timeout']);
61
        return $process->isSuccessful();
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    protected function getOptions()
68
    {
69
        $options = array_merge(
70
            ['cmd' => '', 'desc' => '', 'timeout' => 120],
71
            $this->options
72
        );
73
74
        return $options;
75
    }
76
}
77