Passed
Push — siad007-patch-1 ( de6eed...cff1f5 )
by Siad
09:57 queued 04:34
created

TryCatchTask::main()   B

Complexity

Conditions 8
Paths 69

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 8.0109

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
ccs 17
cts 18
cp 0.9444
rs 8.4444
c 0
b 0
f 0
cc 8
nc 69
nop 0
crap 8.0109
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\Task\System;
21
22
use Exception;
23
use Phing\Exception\BuildException;
24
use Phing\Task;
25
26
/**
27
 * A wrapper task that lets you run tasks(s) when another set
28
 * of tasks fails.
29
 *
30
 * Inspired by {@link http://ant-contrib.sourceforge.net/tasks/tasks/trycatch.html}
31
 *
32
 * @author  Michiel Rook <[email protected]>
33
 */
34
class TryCatchTask extends Task
35
{
36
    protected $propertyName = "";
37
    protected $referenceName = '';
38
39
    protected $tryContainer = null;
40
    protected $catchContainer = null;
41
    protected $finallyContainer = null;
42
43
    /**
44
     * Main method
45
     *
46
     * @throws BuildException
47
     */
48 4
    public function main()
49
    {
50 4
        $exc = null;
51
52 4
        if (empty($this->tryContainer)) {
53
            throw new BuildException('A nested <try> element is required');
54
        }
55
56
        try {
57 4
            $this->tryContainer->perform();
58 4
        } catch (BuildException $e) {
59 4
            if (!empty($this->propertyName)) {
60 4
                $this->project->setProperty($this->propertyName, $e->getMessage());
61
            }
62
63 4
            if (!empty($this->referenceName)) {
64 3
                $this->project->addReference($this->referenceName, $e);
65
            }
66
67 4
            if (!empty($this->catchContainer)) {
68 3
                $this->catchContainer->perform();
69
            } else {
70 1
                $exc = $e;
71
            }
72 2
        } finally {
73 4
            if (!empty($this->finallyContainer)) {
74 4
                $this->finallyContainer->perform();
75
            }
76
        }
77
78 2
        if (!empty($exc)) {
79 1
            throw $exc;
80
        }
81 1
    }
82
83
    /**
84
     * Sets the name of the property that will
85
     * contain the exception message.
86
     *
87
     * @param string $property
88
     */
89 4
    public function setProperty($property)
90
    {
91 4
        $this->propertyName = (string) $property;
92 4
    }
93
94
    /**
95
     * Sets the name of the reference that will
96
     * contain the exception.
97
     *
98
     * @param Exception $reference
99
     *
100
     */
101 3
    public function setReference($reference)
102
    {
103 3
        $this->referenceName = $reference;
104 3
    }
105
106
    /**
107
     * Add nested <try> element
108
     *
109
     */
110 4
    public function addTry(SequentialTask $container)
111
    {
112 4
        $this->tryContainer = $container;
113 4
    }
114
115
    /**
116
     * Add nested <catch> element
117
     *
118
     */
119 3
    public function addCatch(SequentialTask $container)
120
    {
121 3
        $this->catchContainer = $container;
122 3
    }
123
124
    /**
125
     * Add nested <finally> element
126
     *
127
     */
128 4
    public function addFinally(SequentialTask $container)
129
    {
130 4
        $this->finallyContainer = $container;
131 4
    }
132
}
133