Passed
Push — master ( a4c5f9...448818 )
by Keoghan
18:30
created

Cli::execRealTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 13
ccs 5
cts 7
cp 0.7143
rs 10
cc 2
nc 2
nop 1
crap 2.0932
1
<?php
2
3
namespace App\Support\Console;
4
5
use App\Support\Contracts\Cli as CliContract;
6
use Symfony\Component\Process\Exception\ProcessFailedException;
7
use Symfony\Component\Process\Process;
8
9
class Cli implements CliContract
10
{
11
    /**
12
     * The process timeout in seconds.
13
     *
14
     * @var int|null
15
     */
16
    protected $timeout = null;
17
18
    /**
19
     * Execute a command.
20
     *
21
     * @param string $command
22
     *
23
     * @return string
24
     */
25 5
    public function exec($command)
26
    {
27 5
        $process = $this->getProcess($command);
28 5
        $process->run();
29
30 5
        return $process->getOutput();
31
    }
32
33
    /**
34
     * Execute a command in real time.
35
     *
36
     * @param string $command
37
     *
38
     * @return int
39
     */
40 1
    public function execRealTime($command)
41
    {
42 1
        $process = $this->getProcess($command);
43
44
        try {
45 1
            $process->mustRun(function ($type, $buffer) {
46 1
                echo $buffer;
47
            });
48
        } catch (ProcessFailedException $e) {
49
            echo $e->getMessage();
50
        }
51
52 1
        return $process->getExitCode();
53
    }
54
55
    /**
56
     * Execute a command and allow the user to interact with it.
57
     *
58
     * @param string $command
59
     *
60
     * @return int
61
     */
62
    public function passthru($command)
63
    {
64
        $process = $this->getProcess($command);
65
66
        try {
67
            $process->setTty(true);
68
            $process->mustRun(function ($type, $buffer) {
69
                echo $buffer;
70
            });
71
        } catch (ProcessFailedException $e) {
72
            echo $e->getMessage();
73
        }
74
75
        return $process->getExitCode();
76
    }
77
78
    /**
79
     * Get a Symfony process object that can execute a command.
80
     *
81
     * @param string $command The command to execute
82
     *
83
     * @return Process
84
     */
85 5
    protected function getProcess($command)
86
    {
87 5
        return Process::fromShellCommandline($command)
88 5
            ->setTimeout($this->timeout);
89
    }
90
91
    /**
92
     * Return the current working directory.
93
     *
94
     * @return string
95
     */
96 2
    public function currentWorkingDirectory()
97
    {
98 2
        return getcwd();
99
    }
100
101
    /**
102
     * Set the timeout for the wrapping PHP Process.
103
     *
104
     * @param int $seconds
105
     *
106
     * @return Cli
107
     */
108 213
    public function setTimeout(int $seconds)
109
    {
110 213
        $this->timeout = $seconds;
111
112 213
        return $this;
113
    }
114
115
    /**
116
     * Remove the timeout for the wrapping PHP Process.
117
     *
118
     * @return Cli
119
     */
120 3
    public function doNotTimeout()
121
    {
122 3
        $this->timeout = null;
123
124 3
        return $this;
125
    }
126
127
    /**
128
     * Return the timeout for the wrapping PHP Process.
129
     *
130
     * @return int|null
131
     */
132 4
    public function getTimeout()
133
    {
134 4
        return $this->timeout;
135
    }
136
}
137