Passed
Push — master ( 503f62...3f0369 )
by Tom
13:13 queued 10:28
created

RunOpts::getOptionImplementation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Runner;
6
7
use Ktomk\Pipelines\Utility\Options;
8
use Ktomk\Pipelines\Value\Prefix;
9
10
/**
11
 * Runner options parameter object
12
 */
13
class RunOpts
14
{
15
    /**
16
     * @var string
17
     */
18
    private $prefix;
19
20
    /**
21
     * @var null|Options
22
     */
23
    private $options;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $binaryPackage;
29
30
    /**
31
     * @var null|string of step expression or null if not set
32
     */
33
    private $steps;
34
35
    /**
36
     * @var bool
37
     */
38
    private $noManual = false;
39
40
    /**
41
     * @var null|string --user $(id -u):$(id -g)
42
     */
43
    private $user;
44
45
    /**
46
     * @var null|true --ssh-auth
47
     */
48
    private $ssh;
49
50
    /**
51
     * Static factory method
52
     *
53
     * @param string $prefix [optional]
54
     * @param null|string $binaryPackage [optional] package name or path to binary (string)
55
     * @param Options $options [optional]
56
     *
57
     * @return RunOpts
58
     */
59 1
    public static function create($prefix = null, $binaryPackage = null, Options $options = null)
60
    {
61 1
        null === $options && $options = Options::create();
62
63 1
        return new self($prefix, $options, $binaryPackage);
64
    }
65
66
    /**
67
     * RunOpts constructor.
68
     *
69
     * NOTE: All run options are optional by design (pass NULL), some have defaults
70
     *       for that, some need to be initialized/set (marked [setter]) before
71
     *       dedicated use.
72
     *
73
     * @param null|string $prefix [optional] null for default, string for prefix
74
     * @param null|Options $options [optional]
75
     * @param string $binaryPackage [optional][setter] package name or path to binary (string)
76
     * @param null|string $user [optional] user option, non-null (string) if set
77
     * @param null|true $ssh [optional] ssh support for runner, null for none, true for support of ssh agent
78
     */
79 2
    public function __construct(
80
        $prefix = null,
81
        Options $options = null,
82
        $binaryPackage = null,
83
        $user = null,
84
        $ssh = null
85
    ) {
86 2
        $this->setPrefix($prefix);
87 2
        $this->options = $options;
88 2
        $this->binaryPackage = $binaryPackage;
89 2
        $this->user = $user;
90 2
        $this->ssh = $ssh;
91 2
    }
92
93
    /**
94
     * @param string $prefix
95
     *
96
     * @return void
97
     */
98 3
    public function setPrefix($prefix = null)
99
    {
100 3
        $this->prefix = Prefix::filter($prefix);
101 3
    }
102
103
    /**
104
     * The prefix is used when creating containers for the container name.
105
     *
106
     * See --prefix option.
107
     *
108
     * @return string
109
     */
110 1
    public function getPrefix()
111
    {
112 1
        return $this->prefix;
113
    }
114
115
    /**
116
     *
117
     * FIXME(tk): technically the underlying $this->options->get() can also
118
     *            return bool, this is somewhat not fully implemented and
119
     *            should perhaps be delegated {@see getBoolOption()}.
120
     *
121
     * @param string $name
122
     *
123
     * @return string
124
     */
125 2
    public function getOption($name)
126
    {
127 2
        $buffer = $this->getOptionImplementation($name);
128
129 1
        if (is_bool($buffer)) {
130
            // @codeCoverageIgnoreStart
131
            throw new \BadMethodCallException(
132
                sprintf("use bool for option: '%s'", $name)
133
            );
134
            // @codeCoverageIgnoreEnd
135
        }
136
137 1
        return $buffer;
138
    }
139
140
    /**
141
     * @param string $name
142
     *
143
     * @return bool
144
     */
145 1
    public function getBoolOption($name)
146
    {
147 1
        return (bool)$this->getOptionImplementation($name);
148
    }
149
150
    /**
151
     * @param string $binaryPackage
152
     *
153
     * @return void
154
     */
155 1
    public function setBinaryPackage($binaryPackage)
156
    {
157 1
        $this->binaryPackage = $binaryPackage;
158 1
    }
159
160
    /**
161
     * @return string
162
     */
163 1
    public function getBinaryPackage()
164
    {
165 1
        if (null === $this->binaryPackage) {
166
            // @codeCoverageIgnoreStart
167
            throw new \BadMethodCallException('binary-package not yet initialized');
168
            // @codeCoverageIgnoreEnd
169
        }
170
171 1
        return $this->binaryPackage;
172
    }
173
174
    /**
175
     * @return null|string
176
     */
177 1
    public function getSteps()
178
    {
179 1
        return $this->steps;
180
    }
181
182
    /**
183
     * @param null|string $steps
184
     *
185
     * @return void
186
     */
187 1
    public function setSteps($steps)
188
    {
189 1
        $this->steps = $steps;
190 1
    }
191
192
    /**
193
     * @return bool
194
     */
195 1
    public function isNoManual()
196
    {
197 1
        return $this->noManual;
198
    }
199
200
    /**
201
     * @param bool $noManual
202
     *
203
     * @return void
204
     */
205 1
    public function setNoManual($noManual)
206
    {
207 1
        $this->noManual = (bool)$noManual;
208 1
    }
209
210
    /**
211
     * @return null|string
212
     */
213 1
    public function getUser()
214
    {
215 1
        return $this->user;
216
    }
217
218
    /**
219
     * @param null|string $user
220
     *
221
     * @return void
222
     */
223 1
    public function setUser($user)
224
    {
225 1
        $this->user = $user;
226 1
    }
227
228
    /**
229
     * @return null|true
230
     */
231 1
    public function getSsh()
232
    {
233 1
        return $this->ssh ? true : null;
234
    }
235
236
    /**
237
     * @param null|true $ssh
238
     *
239
     * @return void
240
     */
241 1
    public function setSsh($ssh)
242
    {
243 1
        $this->ssh = $ssh ? true : null;
244 1
    }
245
246
    /**
247
     * @param string $name
248
     *
249
     * @return bool|string
250
     */
251 3
    private function getOptionImplementation($name)
252
    {
253 3
        if (!isset($this->options)) {
254 1
            throw new \BadMethodCallException('no options');
255
        }
256
257 2
        $buffer = $this->options->get($name);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

257
        /** @scrutinizer ignore-call */ 
258
        $buffer = $this->options->get($name);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
258 2
        if (null === $buffer) {
259 2
            throw new \InvalidArgumentException(
260 2
                sprintf("unknown option: '%s'", $name)
261
            );
262
        }
263
264 2
        return $buffer;
265
    }
266
}
267