Passed
Push — master ( 8f57f3...57a8f4 )
by Keoghan
04:05
created

Cli::getProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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
    const TIMEOUT = 120;
15
16
    /**
17
     * Execute a command.
18
     *
19
     * @param string $command
20
     *
21
     * @return string
22
     */
23 5
    public function exec($command)
24
    {
25 5
        $process = $this->getProcess($command);
26 5
        $process->run();
27
28 5
        return $process->getOutput();
29
    }
30
31
    /**
32
     * Execute a command in real time.
33
     *
34
     * @param string $command
35
     *
36
     * @return string
37
     */
38 2
    public function execRealTime($command)
39
    {
40 2
        $process = $this->getProcess($command);
41
42
        try {
43
            $process->mustRun(function ($type, $buffer) {
44 1
                echo $buffer;
45 2
            });
46
        } catch (ProcessFailedException $e) {
47
            echo $e->getMessage();
48
        }
49 2
    }
50
51
    /**
52
     * Execute a command and allow the user to interact with it.
53
     *
54
     * @param string $command
55
     *
56
     * @return void
57
     */
58 1
    public function passthru($command)
59
    {
60 1
        $process = $this->getProcess($command);
61
62
        try {
63 1
            $process->setTty(true);
64
            $process->mustRun(function ($type, $buffer) {
65
                echo $buffer;
66 1
            });
67
        } catch (ProcessFailedException $e) {
68
            echo $e->getMessage();
69
        }
70 1
    }
71
72
    /**
73
     * Get a Symfony process object that can execute a command.
74
     *
75
     * @param string $command The command to execute
76
     *
77
     * @return Process
78
     */
79 7
    protected function getProcess($command)
80
    {
81 7
        return app()->make(Process::class, [
82 7
            'commandline' => $command,
83 7
            'timeout'     => static::TIMEOUT,
84
        ]);
85
    }
86
87
    /**
88
     * Return the current working directory.
89
     *
90
     * @return string
91
     */
92 1
    public function currentWorkingDirectory()
93
    {
94 1
        return getcwd();
95
    }
96
}
97