Passed
Push — master ( 94214e...f603ad )
by Andrés
58s
created

ExecTask::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
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 31
    public function getName()
28
    {
29 31
        return 'exec';
30
    }
31
32
    /**
33
     * @return string
34
     */
35 3
    public function getDescription()
36
    {
37 3
        $options = $this->getOptions();
38
39 3
        if ($options['desc']) {
40 2
            return '[Exec] ' . $options['desc'];
41
        }
42
43 1
        return '[Exec] Custom command';
44
    }
45
46
    /**
47
     * @return bool
48
     *
49
     * @throws ErrorException
50
     */
51 3
    public function execute()
52
    {
53 3
        $options = $this->getOptions();
54
55 3
        if (!$options['cmd']) {
56 1
            throw new ErrorException('Parameter "cmd" is not defined');
57
        }
58
59
        /** @var Process $process */
60 2
        $process = $this->runtime->runCommand($options['cmd'], $options['timeout']);
61 2
        return $process->isSuccessful();
62
    }
63
64
    /**
65
     * @return array
66
     */
67 3
    protected function getOptions()
68
    {
69 3
        $options = array_merge(
70 3
            ['cmd' => '', 'desc' => '', 'timeout' => 120],
71 3
            $this->options
72 3
        );
73
74 3
        return $options;
75
    }
76
}
77