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

SwitchTask   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 106
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 3 1
A addDefault() 0 7 2
A setCaseInsensitive() 0 3 1
A addCase() 0 3 1
B main() 0 38 10
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\Task;
24
25
/**
26
 * Task definition for the phing task to switch on a particular value.
27
 *
28
 * Task calling syntax:
29
 * ```
30
 * <switch value="value" [caseinsensitive="true|false"] >
31
 *   <case value="val">
32
 *     <property name="propname" value="propvalue" /> |
33
 *     <phingcall target="targetname" /> |
34
 *     any other tasks
35
 *   </case>
36
 *   [
37
 *   <default>
38
 *     <property name="propname" value="propvalue" /> |
39
 *     <phingcall target="targetname" /> |
40
 *     any other tasks
41
 *   </default>
42
 *   ]
43
 * </switch>
44
 * ```
45
 *
46
 *
47
 * Attributes:
48
 * value           -> The value to switch on
49
 * caseinsensitive -> Should we do case insensitive comparisons?
50
 *                    (default is false)
51
 *
52
 * Subitems:
53
 * case     --> An individual case to consider, if the value that
54
 *              is being switched on matches to value attribute of
55
 *              the case, then the nested tasks will be executed.
56
 * default  --> The default case for when no match is found.
57
 *
58
 *
59
 * Crude Example:
60
 *
61
 * ```
62
 * <switch value="${foo}">
63
 *     <case value="bar">
64
 *       <echo message="The value of property foo is bar" />
65
 *     </case>
66
 *     <case value="baz">
67
 *       <echo message="The value of property foo is baz" />
68
 *     </case>
69
 *     <default>
70
 *       <echo message="The value of property foo is not sensible" />
71
 *     </default>
72
 * </switch>
73
 * ```
74
 *
75
 * @author  Siad Ardroumli <[email protected]>
76
 * @package phing.tasks.system
77
 */
78
class SwitchTask extends Task
79
{
80
    /**
81
     * @var mixed $value
82
     */
83
    private $value = null;
84
85
    /**
86
     * @var array $cases
87
     */
88
    private $cases = [];
89
90
    /**
91
     * @var SequentialTask $defaultCase
92
     */
93
    private $defaultCase = null;
94
95
    /**
96
     * @var bool $caseInsensitive
97
     */
98
    private $caseInsensitive = false;
99
100
    /***
101
     * Sets the value being switched on.
102
     *
103
     * @param mixed $value
104
     *
105
     * @return void
106
     */
107
    public function setValue($value)
108
    {
109
        $this->value = $value;
110
    }
111
112
    /**
113
     * Adds a CaseTask.
114
     *
115
     * @param CaseTask $case
116
     *
117
     * @return void
118
     */
119
    public function addCase(CaseTask $case)
120
    {
121
        $this->cases[] = $case;
122
    }
123
124
    /**
125
     * @param $bool
126
     *
127
     * @return void
128
     */
129
    public function setCaseInsensitive($bool)
130
    {
131
        $this->caseInsensitive = $bool;
132
    }
133
134
    /**
135
     * Creates the `<default>` tag
136
     */
137
    public function addDefault(SequentialTask $res)
138
    {
139
        if ($this->defaultCase !== null) {
140
            throw new BuildException("Cannot specify multiple default cases");
141
        }
142
143
        $this->defaultCase = $res;
144
    }
145
146
    public function main()
147
    {
148
        if ($this->value === null) {
149
            throw new BuildException("Value is missing <switch>");
150
        }
151
152
        if (empty($this->cases) && $this->defaultCase === null) {
153
            throw new BuildException("No cases supplied <switch>");
154
        }
155
156
        $selectedCase = $this->defaultCase;
157
158
        /**
159
         * @var CaseTask $case
160
         */
161
        foreach ($this->cases as $case) {
162
            $cValue = $case->getValue();
163
164
            if (empty($case)) {
165
                throw new BuildException("Value is required for case.");
166
            }
167
168
            $mValue = $this->value;
169
            if ($this->caseInsensitive) {
170
                $cValue = strtoupper($case->getValue());
171
                $mValue = strtoupper($this->value);
172
            }
173
174
            if ($cValue === $mValue && $case != $this->defaultCase) {
175
                $selectedCase = $case;
176
            }
177
        }
178
179
        if ($selectedCase === null) {
180
            throw new BuildException("No case matched the value " . $this->value . " and no default has been specified.");
181
        }
182
183
        $selectedCase->perform();
184
    }
185
}
186