Passed
Push — master ( 48eab7...6ad25b )
by Keoghan
05:02
created

CliCommand::bash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace App\Support\Console\DockerCompose;
4
5
use App\PorterLibrary;
6
use App\Support\Contracts\Cli;
7
8
class CliCommand
9
{
10
    protected $command = '';
11
    protected $interactive = false;
12
    protected $realTime = false;
13
14
    /**
15
     * @var Cli
16
     */
17
    protected $cli;
18
19 27
    public function __construct(Cli $cli, $command)
20
    {
21 27
        $this->command = trim($command);
22 27
        $this->cli = $cli;
23 27
    }
24
25
    /**
26
     * Append a bash command, optionally with a further call.
27
     *
28
     * @param string|null $command
29
     *
30
     * @return $this
31
     */
32 3
    public function bash($command = null)
33
    {
34 3
        $this->interactive();
35 3
        $this->append('bash');
36
37 3
        if ($command) {
38 1
            $this->append("-c \"$command\"");
39
        }
40
41 3
        return $this;
42
    }
43
44
    /**
45
     * Append to a command.
46
     *
47
     * @param string|null $string
48
     *
49
     * @return $this
50
     */
51 4
    public function append($string = null)
52
    {
53 4
        $this->command = trim($this->command." {$string}");
54
55 4
        return $this;
56
    }
57
58
    /**
59
     * Set a command as being interactive (i.e. passthru() in php).
60
     *
61
     * @return $this
62
     */
63 6
    public function interactive()
64
    {
65 6
        $this->interactive = true;
66
67 6
        return $this;
68
    }
69
70
    /**
71
     * Set a command as not being interactive.
72
     *
73
     * @return $this
74
     */
75 1
    public function notInteractive()
76
    {
77 1
        $this->interactive = false;
78
79 1
        return $this;
80
    }
81
82
    /**
83
     * Check if the command is expected to be interactive.
84
     *
85
     * @return bool
86
     */
87 16
    public function isInteractive()
88
    {
89 16
        return $this->interactive;
90
    }
91
92
    /**
93
     * Set our expectation to see real-time output.
94
     *
95
     * @return $this
96
     */
97 9
    public function realTime()
98
    {
99 9
        $this->realTime = true;
100
101 9
        return $this;
102
    }
103
104
    /**
105
     * Set our expectation NOT to see real-time output.
106
     *
107
     * @return $this
108
     */
109 2
    public function notRealTime()
110
    {
111 2
        $this->realTime = false;
112
113 2
        return $this;
114
    }
115
116
    /**
117
     * Check if we're expecting real0time output.
118
     *
119
     * @return bool
120
     */
121 15
    public function isRealTime()
122
    {
123 15
        return $this->realTime;
124
    }
125
126
    /**
127
     * Prepare the full command string.
128
     *
129
     * @return string
130
     */
131 21
    public function prepare()
132
    {
133 21
        return trim(
134
            'docker-compose -f '
135 21
            .app(PorterLibrary::class)->dockerComposeFile()
136 21
            .' -p porter '
137 21
            .$this->command
138
        );
139
    }
140
141
    /**
142
     * Execute the command.
143
     *
144
     * @return string|null
145
     */
146 13
    public function perform()
147
    {
148 13
        if ($this->isInteractive()) {
149 1
            $this->cli->passthru($this->prepare());
150
151 1
            return;
152
        }
153
154 13
        if ($this->isRealTime()) {
155 7
            return $this->cli->execRealTime($this->prepare());
156
        }
157
158 11
        return $this->cli->exec($this->prepare());
159
    }
160
}
161