Passed
Pull Request — master (#5)
by Aleksandr
03:12
created

Command   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 0 23 5
1
<?php
2
3
namespace Sanchescom\WiFi\System;
4
5
use InvalidArgumentException;
6
use Sanchescom\WiFi\Exceptions\CommandException;
7
8
/**
9
 * Class CommandExecutor.
10
 */
11
class Command
12
{
13
    /**
14
     * @param string $command
15
     * @param bool   $mergeStdErr
16
     *
17
     * @return string
18
     */
19
    public function execute(string $command, bool $mergeStdErr = true)
20
    {
21
        if (empty($command)) {
22
            throw new InvalidArgumentException('Command line is empty');
23
        }
24
25
        if ($mergeStdErr) {
26
            $command .= ' 2>&1';
27
        }
28
29
        exec($command, $output, $code);
30
31
        if (count($output) === 0) {
32
            $output = $code;
33
        } else {
34
            $output = implode(PHP_EOL, $output);
35
        }
36
37
        if ($code !== 0) {
38
            throw new CommandException($command, $output, $code);
39
        }
40
41
        return $output;
42
    }
43
}
44