Test Failed
Branch feature__set_up_scrutinizer (ea6624)
by Robin
06:04 queued 02:46
created

CliCommand::notRealTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
147
        }
148
149 10
        if ($this->isRealTime()) {
150 6
            return $this->cli->execRealTime($this->prepare());
151
        }
152
153 8
        return $this->cli->exec($this->prepare());
154
    }
155
}
156