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

HasFreeSpaceCondition::setNeeded()   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\Condition;
21
22
use Phing\Exception\BuildException;
23
use Phing\Util\SizeHelper;
24
25
/**
26
 * Condition returns true if selected partition has the requested space, false otherwise.
27
 *
28
 * @author  Siad Ardroumli <[email protected]>
29
 * @package phing.tasks.system.condition
30
 */
31
class HasFreeSpaceCondition implements Condition
32
{
33
    /**
34
     * @var string $partition
35
     */
36
    private $partition;
37
38
    /**
39
     * @var string $needed
40
     */
41
    private $needed;
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @throws BuildException
47
     *
48
     * @return boolean
49
     */
50
    public function evaluate()
51
    {
52
        $this->validate();
53
54
        $free = disk_free_space($this->partition);
55
        return $free >= SizeHelper::fromHumanToBytes($this->needed);
56
    }
57
58
    /**
59
     * @return void
60
     *
61
     * @throws BuildException
62
     */
63
    private function validate()
64
    {
65
        if (null == $this->partition) {
66
            throw new BuildException("Please set the partition attribute.");
67
        }
68
        if (null == $this->needed) {
69
            throw new BuildException("Please set the needed attribute.");
70
        }
71
    }
72
73
    /**
74
     * Set the partition/device to check.
75
     *
76
     * @param $partition
77
     *
78
     * @return void
79
     */
80
    public function setPartition($partition)
81
    {
82
        $this->partition = $partition;
83
    }
84
85
    /**
86
     * Set the amount of free space required.
87
     *
88
     * @return void
89
     */
90
    public function setNeeded($needed)
91
    {
92
        $this->needed = $needed;
93
    }
94
}
95