Passed
Push — main ( f8c128...609b6a )
by Siad
05:26
created

SwitchTask::addCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\System;
22
23
use Phing\Exception\BuildException;
24
use Phing\Task;
25
26
/**
27
 * Task definition for the phing task to switch on a particular value.
28
 *
29
 * Task calling syntax:
30
 * ```
31
 * <switch value="value" [caseinsensitive="true|false"] >
32
 *   <case value="val">
33
 *     <property name="propname" value="propvalue" /> |
34
 *     <phingcall target="targetname" /> |
35
 *     any other tasks
36
 *   </case>
37
 *   [
38
 *   <default>
39
 *     <property name="propname" value="propvalue" /> |
40
 *     <phingcall target="targetname" /> |
41
 *     any other tasks
42
 *   </default>
43
 *   ]
44
 * </switch>
45
 * ```
46
 *
47
 *
48
 * Attributes:
49
 * value           -> The value to switch on
50
 * caseinsensitive -> Should we do case insensitive comparisons?
51
 *                    (default is false)
52
 *
53
 * Subitems:
54
 * case     --> An individual case to consider, if the value that
55
 *              is being switched on matches to value attribute of
56
 *              the case, then the nested tasks will be executed.
57
 * default  --> The default case for when no match is found.
58
 *
59
 *
60
 * Crude Example:
61
 *
62
 * ```
63
 * <switch value="${foo}">
64
 *     <case value="bar">
65
 *       <echo message="The value of property foo is bar" />
66
 *     </case>
67
 *     <case value="baz">
68
 *       <echo message="The value of property foo is baz" />
69
 *     </case>
70
 *     <default>
71
 *       <echo message="The value of property foo is not sensible" />
72
 *     </default>
73
 * </switch>
74
 * ```
75
 *
76
 * @author  Siad Ardroumli <[email protected]>
77
 */
78
class SwitchTask extends Task
79
{
80
    private $value;
81
82
    /**
83
     * @var array
84
     */
85
    private $cases = [];
86
87
    /**
88
     * @var SequentialTask
89
     */
90
    private $defaultCase;
91
92
    /**
93
     * @var bool
94
     */
95
    private $caseInsensitive = false;
96
97
    /*
98
     * Sets the value being switched on.
99
     *
100
     * @param mixed $value
101
     *
102
     * @return void
103
     */
104 2
    public function setValue($value)
105
    {
106 2
        $this->value = $value;
107 2
    }
108
109
    /**
110
     * Adds a CaseTask.
111
     */
112 2
    public function addCase(CaseTask $case)
113
    {
114 2
        $this->cases[] = $case;
115 2
    }
116
117
    /**
118
     * @param bool $caseInsensitive
119
     */
120
    public function setCaseInsensitive($caseInsensitive)
121
    {
122
        $this->caseInsensitive = $caseInsensitive;
123
    }
124
125
    /**
126
     * Creates the `<default>` tag.
127
     */
128 2
    public function addDefault(SequentialTask $res)
129
    {
130 2
        if (null !== $this->defaultCase) {
131
            throw new BuildException('Cannot specify multiple default cases');
132
        }
133
134 2
        $this->defaultCase = $res;
135 2
    }
136
137 2
    public function main()
138
    {
139 2
        if (null === $this->value) {
140
            throw new BuildException('Value is missing <switch>');
141
        }
142
143 2
        if (empty($this->cases) && null === $this->defaultCase) {
144
            throw new BuildException('No cases supplied <switch>');
145
        }
146
147 2
        $selectedCase = $this->defaultCase;
148
149
        /**
150
         * @var CaseTask $case
151
         */
152 2
        foreach ($this->cases as $case) {
153 2
            $cValue = $case->getValue();
154
155 2
            if (empty($case)) {
156
                throw new BuildException('Value is required for case.');
157
            }
158
159 2
            $mValue = $this->value;
160 2
            if ($this->caseInsensitive) {
161
                $cValue = strtoupper($case->getValue());
162
                $mValue = strtoupper($this->value);
163
            }
164
165 2
            if ($cValue === $mValue && $case != $this->defaultCase) {
166 1
                $selectedCase = $case;
167
            }
168
        }
169
170 2
        if (null === $selectedCase) {
171
            throw new BuildException('No case matched the value ' . $this->value . ' and no default has been specified.');
172
        }
173
174 2
        $selectedCase->perform();
175 2
    }
176
}
177