Passed
Pull Request — master (#41)
by Keoghan
03:35
created

Cli::execRealTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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
     * Execute a command.
13
     *
14
     * @param string $command
15
     *
16
     * @return string
17
     */
18 2
    public function exec($command)
19
    {
20 2
        $process = new Process($command);
21 2
        $process->run();
22
23 2
        return $process->getOutput();
24
    }
25
26
    /**
27
     * Execute a command in real time.
28
     *
29
     * @param string $command
30
     *
31
     * @return string
32
     */
33
    public function execRealTime($command)
34
    {
35
        $process = new Process($command);
36
37
        try {
38
            $process->mustRun(function ($type, $buffer) {
39
                echo $buffer;
40
            });
41
        } catch (ProcessFailedException $e) {
42
            echo $e->getMessage();
43
        }
44
    }
45
46
    /**
47
     * Execute a command and allow the user to interact with it.
48
     *
49
     * @param string $command
50
     *
51
     * @return void
52
     */
53
    public function passthru($command)
54
    {
55
        $process = new Process($command);
56
57
        try {
58
            $process->setTty(true);
59
            $process->mustRun(function ($type, $buffer) {
60
                echo $buffer;
61
            });
62
        } catch (ProcessFailedException $e) {
63
            echo $e->getMessage();
64
        }
65
    }
66
67
    /**
68
     * Return the current working directory.
69
     *
70
     * @return string
71
     */
72 1
    public function currentWorkingDirectory()
73
    {
74 1
        return getcwd();
75
    }
76
}
77