Passed
Push — master ( 3ee5ba...23bbf0 )
by Keoghan
04:26
created

CliCommand::interactive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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