ExecTask::getDescription()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Task\BuiltIn;
13
14
use Mage\Task\Exception\ErrorException;
15
use Mage\Task\AbstractTask;
16
use Symfony\Component\Process\Process;
17
18
/**
19
 * Exec task. Allows you to execute arbitrary commands.
20
 *
21
 * @author Yanick Witschi <https://github.com/Toflar>
22
 */
23
class ExecTask extends AbstractTask
24
{
25
    /**
26
     * @return string
27
     */
28 48
    public function getName(): string
29
    {
30 48
        return 'exec';
31
    }
32
33
    /**
34
     * @return string
35
     */
36 4
    public function getDescription(): string
37
    {
38 4
        $options = $this->getOptions();
39
40 4
        if ($options['desc']) {
41 3
            return '[Exec] ' . $options['desc'];
42
        }
43
44 1
        return '[Exec] Custom command';
45
    }
46
47
    /**
48
     * @return bool
49
     *
50
     * @throws ErrorException
51
     */
52 4
    public function execute(): bool
53
    {
54 4
        $options = $this->getOptions();
55
56 4
        if (!$options['cmd']) {
57 1
            throw new ErrorException('Parameter "cmd" is not defined');
58
        }
59
60 3
        $mapping = [
61 3
            '%environment%' => $this->runtime->getEnvironment(),
62
        ];
63
64 3
        if ($this->runtime->getReleaseId() !== null) {
65 1
            $mapping['%release%'] = $this->runtime->getReleaseId();
66
        }
67
68 3
        $cmd = str_replace(
69 3
            array_keys($mapping),
70 3
            array_values($mapping),
71 3
            strval($options['cmd'])
72
        );
73
74
        /** @var Process $process */
75 3
        $process = $this->runtime->runCommand($cmd, intval($options['timeout']));
76 3
        return $process->isSuccessful();
77
    }
78
79
    /**
80
     * @return array<string, string|int>
81
     */
82 4
    protected function getOptions(): array
83
    {
84 4
        $options = array_merge(
85 4
            ['cmd' => '', 'desc' => '', 'timeout' => 120],
86 4
            $this->options
87
        );
88
89 4
        return $options;
90
    }
91
}
92