Passed
Push — master ( caf93d...cf5135 )
by Keoghan
06:09
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 6
    public function exec($command)
26
    {
27 6
        $process = $this->getProcess($command);
28 6
        $process->run();
29
30 6
        return $process->getOutput();
31
    }
32
33
    /**
34
     * Execute a command in real time.
35
     *
36
     * @param string $command
37
     *
38
     * @return int
39
     */
40 2
    public function execRealTime($command)
41
    {
42 2
        $process = $this->getProcess($command);
43
44
        try {
45
            $process->mustRun(function ($type, $buffer) {
46 1
                echo $buffer;
47 2
            });
48
        } catch (ProcessFailedException $e) {
49
            echo $e->getMessage();
50
        }
51
52 2
        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 1
    public function passthru($command)
63
    {
64 1
        $process = $this->getProcess($command);
65
66
        try {
67 1
            $process->setTty(true);
68
            $process->mustRun(function ($type, $buffer) {
69
                echo $buffer;
70 1
            });
71
        } catch (ProcessFailedException $e) {
72
            echo $e->getMessage();
73
        }
74
75 1
        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 8
    protected function getProcess($command)
86
    {
87 8
        return app()->make(Process::class, [
88 8
            'command'     => $command,
89 8
            'timeout'     => $this->timeout,
90
        ]);
91
    }
92
93
    /**
94
     * Return the current working directory.
95
     *
96
     * @return string
97
     */
98 2
    public function currentWorkingDirectory()
99
    {
100 2
        return getcwd();
101
    }
102
103
    /**
104
     * Set the timeout for the wrapping PHP Process.
105
     *
106
     * @param int $seconds
107
     *
108
     * @return Cli
109
     */
110 206
    public function setTimeout(int $seconds)
111
    {
112 206
        $this->timeout = $seconds;
113
114 206
        return $this;
115
    }
116
117
    /**
118
     * Remove the timeout for the wrapping PHP Process.
119
     *
120
     * @return Cli
121
     */
122 3
    public function doNotTimeout()
123
    {
124 3
        $this->timeout = null;
125
126 3
        return $this;
127
    }
128
129
    /**
130
     * Return the timeout for the wrapping PHP Process.
131
     *
132
     * @return int|null
133
     */
134 4
    public function getTimeout()
135
    {
136 4
        return $this->timeout;
137
    }
138
}
139