Command::execute()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Sanchescom\WiFi\System;
4
5
use Sanchescom\WiFi\Contracts\CommandInterface;
6
use Sanchescom\WiFi\Exceptions\CommandException;
7
8
/**
9
 * Class CommandExecutor.
10
 */
11
class Command implements CommandInterface
12
{
13
    /** @var string */
14
    protected $lastCommand;
15
16
    /**
17
     * @param string $command
18
     *
19
     * @return string
20
     */
21
    public function execute(string $command)
22
    {
23
        $command .= ' 2>&1';
24
25
        exec($command, $output, $code);
26
27
        $output = $this->lastCommand = count($output) === 0
28
            ? $code
29
            : implode(PHP_EOL, $output);
30
31
        if ($code !== 0) {
32
            throw new CommandException($command, $output, $code);
33
        }
34
35
        return $output;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getLastCommand()
42
    {
43
        return $this->lastCommand;
44
    }
45
}
46