Failed Conditions
Pull Request — master (#82)
by Keoghan
07:02
created

CliCommand   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
eloc 38
dl 0
loc 177
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A realTime() 0 5 1
A isInteractive() 0 3 1
A prepare() 0 7 1
A isRealTime() 0 3 1
A interactive() 0 5 1
A notInteractive() 0 5 1
A notRealTime() 0 5 1
A append() 0 5 1
A bash() 0 10 2
A perform() 0 13 3
A setTimeout() 0 5 1
A getCli() 0 3 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 28
    public function __construct(Cli $cli, $command)
20
    {
21 28
        $this->command = trim($command);
22 28
        $this->cli = $cli;
23 28
    }
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
143
    /**
144
     * Execute the command.
145
     *
146
     * @return string|null
147
     */
148 13
    public function perform()
149
    {
150 13
        if ($this->isInteractive()) {
151 1
            $this->cli->passthru($this->prepare());
152
153 1
            return;
154
        }
155
156 13
        if ($this->isRealTime()) {
157 7
            return $this->cli->execRealTime($this->prepare());
158
        }
159
160 11
        return $this->cli->exec($this->prepare());
161
    }
162
163
    /**
164
     * Return the Cli instance for this CliCommand.
165
     *
166
     * @return Cli
167
     */
168 1
    public function getCli()
169
    {
170 1
        return $this->cli;
171
    }
172
173
    /**
174
     * Set the timeout for the Cli instance.
175
     *
176
     * @param $seconds
177
     *
178
     * @return CliCommand
179
     */
180 2
    public function setTimeout($seconds)
181
    {
182 2
        $this->cli->setTimeout($seconds);
0 ignored issues
show
Bug introduced by
The method setTimeout() does not exist on App\Support\Contracts\Cli. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Support\Contracts\Cli. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

182
        $this->cli->/** @scrutinizer ignore-call */ 
183
                    setTimeout($seconds);
Loading history...
183
184 2
        return $this;
185
    }
186
}
187