Passed
Push — master ( 06f87c...33b6d3 )
by Siad
08:23
created

FileSizeTask::setUnit()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
/**
21
 * FileSizeTask
22
 *
23
 * Returns the size of a file
24
 *
25
 * @author  Johan Persson <[email protected]>
26
 * @package phing.tasks.ext
27
 */
28
class FileSizeTask extends Task
29
{
30
    /**
31
     * Property for File
32
     *
33
     * @var PhingFile file
34
     */
35
    private $file;
36
37
    /**
38
     * Property where the file size will be stored
39
     *
40
     * @var string
41
     */
42
    private $propertyName = "filesize";
43
44
    /**
45
     * Return size in this unit
46
     *
47
     * @var string
48
     */
49
    private $unit = SizeHelper::B;
50
51
    /**
52
     * Which file to calculate the file size of
53
     *
54
     * @param PhingFile $file
55
     */
56 21
    public function setFile(PhingFile $file)
57
    {
58 21
        if (!$file->canRead()) {
59 1
            throw new BuildException(sprintf('Input file does not exist or is not readable: %s', $file->getName()));
60
        }
61 20
        $this->file = $file;
62 20
    }
63
64
    /**
65
     * Set the name of the property to store the file size
66
     *
67
     * @param  $property
68
     * @return void
69
     */
70 9
    public function setPropertyName(string $property)
71
    {
72 9
        if (empty($property)) {
73 1
            throw new BuildException('Property name cannot be empty');
74
        }
75 8
        $this->propertyName = $property;
76 8
    }
77
78 10
    public function setUnit(string $unit)
79
    {
80 10
        $this->unit = $unit;
81 10
    }
82
83
    /**
84
     * Main-Method for the Task
85
     *
86
     * @return void
87
     * @throws BuildException
88
     */
89 20
    public function main()
90
    {
91 20
        if (!($this->file instanceof PhingFile)) {
0 ignored issues
show
introduced by
$this->file is always a sub-type of PhingFile.
Loading history...
92 1
            throw new BuildException('Input file not specified');
93
        }
94
95 19
        $size = filesize($this->file);
96
97 19
        if ($size === false) {
98
            throw new BuildException(sprintf('Cannot determine filesize of: %s', $this->file));
99
        }
100 19
        $this->log(sprintf('%s filesize is %s%s', $this->file, $size, SizeHelper::B), Project::MSG_VERBOSE);
101
102 19
        $size = SizeHelper::fromBytesTo($size, $this->unit);
103
104 17
        $this->log(sprintf('%s filesize is %s%s', $this->file, $size, $this->unit), Project::MSG_INFO);
105
106 17
        $this->project->setProperty($this->propertyName, $size);
107 17
    }
108
}
109