Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

InputTask::setMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
namespace Phing\Tasks\System;
21
22
use Phing\Exception\BuildException;
23
use Phing\Input\InputRequest;
24
use Phing\Input\MultipleChoiceInputRequest;
25
use Phing\Input\YesNoInputRequest;
26
use Phing\Project;
27
use Phing\Task;
28
use Phing\Util\StringHelper;
29
30
/**
31
 * Reads input from the InputHandler.
32
 *
33
 * @see     Project::getInputHandler()
34
 * @author  Hans Lellelid <[email protected]> (Phing)
35
 * @author  Ulrich Schmidt <[email protected]> (Ant)
36
 * @author  Stefan Bodewig <[email protected]> (Ant)
37
 * @package phing.tasks.system
38
 */
39
class InputTask extends Task
40
{
41
    /**
42
     * @var string
43
     */
44
    private $validargs;
45
46
    /**
47
     * @var string
48
     */
49
    private $message = ""; // required
50
51
    /**
52
     * @var string
53
     */
54
    private $propertyName; // required
55
56
    /**
57
     * @var string
58
     */
59
    private $defaultValue;
60
61
    /**
62
     * @var string
63
     */
64
    private $promptChar;
65
66
    /**
67
     * @var bool
68
     */
69
    private $hidden = false;
70
71
    /**
72
     * Defines valid input parameters as comma separated strings. If set, input
73
     * task will reject any input not defined as accepted and requires the user
74
     * to reenter it. Validargs are case sensitive. If you want 'a' and 'A' to
75
     * be accepted you need to define both values as accepted arguments.
76
     *
77
     * @param string $validargs A comma separated String defining valid input args.
78
     */
79
    public function setValidargs($validargs)
80
    {
81
        $this->validargs = $validargs;
82
    }
83
84
    /**
85
     * Defines the name of a property to be set from input.
86
     *
87
     * @param string $name Name for the property to be set from input
88
     */
89
    public function setPropertyName($name)
90
    {
91
        $this->propertyName = $name;
92
    }
93
94
    /**
95
     * Sets the Message which gets displayed to the user during the build run.
96
     *
97
     * @param string $message The message to be displayed.
98
     */
99
    public function setMessage($message)
100
    {
101
        $this->message = $message;
102
    }
103
104
    /**
105
     * Set a multiline message.
106
     *
107
     * @param string $msg
108
     */
109
    public function addText($msg)
110
    {
111
        $this->message .= $this->project->replaceProperties($msg);
112
    }
113
114
    /**
115
     * Add a default value.
116
     *
117
     * @param string $v
118
     */
119
    public function setDefaultValue($v)
120
    {
121
        $this->defaultValue = $v;
122
    }
123
124
    /**
125
     * Set the character/string to use for the prompt.
126
     *
127
     * @param string $c
128
     */
129
    public function setPromptChar($c)
130
    {
131
        $this->promptChar = $c;
132
    }
133
134
    /**
135
     * @param bool $hidden
136
     */
137
    public function setHidden($hidden)
138
    {
139
        $this->hidden = $hidden;
140
    }
141
142
    /**
143
     * Actual method executed by phing.
144
     *
145
     * @throws BuildException
146
     */
147
    public function main()
148
    {
149
        if ($this->propertyName === null) {
150
            throw new BuildException("You must specify a value for propertyName attribute.");
151
        }
152
153
        if ($this->message === "") {
154
            throw new BuildException("You must specify a message for input task.");
155
        }
156
157
        if ($this->validargs !== null) {
158
            $accept = preg_split('/[\s,]+/', $this->validargs);
159
160
            // is it a boolean (yes/no) inputrequest?
161
            $yesno = false;
162
            if (count($accept) == 2) {
163
                $yesno = true;
164
                foreach ($accept as $ans) {
165
                    if (!StringHelper::isBoolean($ans)) {
166
                        $yesno = false;
167
                        break;
168
                    }
169
                }
170
            }
171
            if ($yesno) {
172
                $request = new YesNoInputRequest($this->message, $accept);
173
            } else {
174
                $request = new MultipleChoiceInputRequest($this->message, $accept);
175
            }
176
        } else {
177
            $request = new InputRequest($this->message);
178
        }
179
180
        // default default is curr prop value
181
        $request->setDefaultValue($this->project->getProperty($this->propertyName));
182
        $request->setPromptChar($this->promptChar);
183
        $request->setHidden($this->hidden);
184
185
        // unless overridden...
186
        if ($this->defaultValue !== null) {
187
            $request->setDefaultValue($this->defaultValue);
188
        }
189
190
        $this->project->getInputHandler()->handleInput($request);
191
192
        $value = $request->getInput();
193
194
        if ($value !== null) {
0 ignored issues
show
introduced by
The condition $value !== null is always true.
Loading history...
195
            $this->project->setUserProperty($this->propertyName, $value);
196
        }
197
    }
198
}
199