CLI::execCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace PressCLI\WPCLI;
3
4
class CLI
5
{
6
    /**
7
     * Executes a WP CLI command.
8
     *
9
     * @param  string $command   The command to execute.
10
     * @param  array  $arguments Optional, command arguments. Default none.
11
     * @param  array  $options   Optional, command options. Default none.
12
     *
13
     * @return string The output from the executed command or NULL if an error occurred or the command produces no output.
14
     */
15
    public static function execCommand($command, $arguments = [], $options = [])
16
    {
17
        $arguments = trim(implode(' ', $arguments));
18
        $options = self::execOptions($options);
19
        $command = trim(PRESS_CLI_WP_EXEC . " {$command}");
20
        $command = implode(' ', array_filter([$command, $arguments, $options]));
21
22
        return system($command);
23
    }
24
25
    /**
26
     * Execution options.
27
     *
28
     * @param  array $options Command options.
29
     *
30
     * @return string         The execution command options.
31
     */
32
    protected static function execOptions($options)
33
    {
34
        $options = array_map(function ($key, $value) {
35
            if ($value) {
36
                return "--{$key}='{$value}'";
37
            }
38
39
            return "--{$key}";
40
        }, array_keys($options), $options);
41
42
        return trim(implode(' ', $options));
43
    }
44
}
45