Passed
Push — master ( 601cfd...7bcbf1 )
by Michiel
22:46
created

FileSizeTask::setFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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