Passed
Push — main ( 58ef3e...be0a65 )
by Michiel
07:16
created

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