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

FileHashTask::setHashtype()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\FileOutputStream;
24
use Phing\Io\File;
25
use Phing\Task;
26
27
/**
28
 * fileHash
29
 *
30
 * Calculate either MD5 or SHA hash value of a specified file and retun the
31
 * value in a property
32
 *
33
 * @author  Johan Persson <[email protected]>
34
 * @package phing.tasks.ext
35
 */
36
class FileHashTask extends Task
37
{
38
    /**
39
     * Property for File
40
     *
41
     * @var File file
42
     */
43
    private $file;
44
45
    /**
46
     * Property to be set
47
     *
48
     * @var string $property
49
     */
50
    private $propertyName = "filehashvalue";
51
52
    /**
53
     * Specify which hash algorithm to use.
54
     *   0 = MD5
55
     *   1 = SHA1
56
     *
57
     * @var integer $hashtype
58
     */
59
    private $hashtype = 0;
60
61
    /** @var string $algorithm */
62
    private $algorithm = '';
63
64
    /**
65
     * Specify if MD5 or SHA1 hash should be used
66
     *
67
     * @param integer $type 0=MD5, 1=SHA1
68
     */
69
    public function setHashtype($type): void
70
    {
71
        $this->hashtype = $type;
72
    }
73
74
    public function setAlgorithm($type): void
75
    {
76
        $this->algorithm = strtolower($type);
77
    }
78
79
    /**
80
     * Which file to calculate the hash value of
81
     *
82
     * @param File $file
83
     */
84
    public function setFile($file): void
85
    {
86
        $this->file = $file;
87
    }
88
89
    /**
90
     * Set the name of the property to store the hash value in
91
     *
92
     * @param  $property
93
     * @return void
94
     */
95
    public function setPropertyName($property): void
96
    {
97
        $this->propertyName = $property;
98
    }
99
100
    /**
101
     * Main-Method for the Task
102
     *
103
     * @throws BuildException
104
     */
105
    public function main()
106
    {
107
        $this->checkFile();
108
        $this->checkPropertyName();
109
110
        // read file
111
        if ($this->algorithm !== '' && in_array($this->algorithm, hash_algos())) {
112
            $this->log("Calculating $this->algorithm hash from: " . $this->file);
113
            $hashValue = hash_file($this->algorithm, $this->file);
114
        } elseif ((int)$this->hashtype === 0) {
115
            $this->log("Calculating MD5 hash from: " . $this->file);
116
            $hashValue = md5_file($this->file, false);
117
            $this->algorithm = 'md5';
118
        } elseif ((int)$this->hashtype === 1) {
119
            $this->log("Calculating SHA1 hash from: " . $this->file);
120
            $hashValue = sha1_file($this->file, false);
121
            $this->algorithm = 'sha1';
122
        } else {
123
            if ($this->algorithm !== '') {
124
                throw new BuildException(
125
                    sprintf(
126
                        '[FileHash] Unknown algorithm specified %d. Must be one of %s',
127
                        $this->algorithm,
128
                        implode(', ', hash_algos())
129
                    )
130
                );
131
            }
132
133
            throw new BuildException(
134
                sprintf(
135
                    '[FileHash] Unknown hashtype specified %d. Must be either 0 (=MD5) or 1 (=SHA1)',
136
                    $this->hashtype
137
                )
138
            );
139
        }
140
141
        // publish hash value
142
        $this->project->setProperty($this->propertyName, $hashValue);
143
        $fos = new FileOutputStream($this->file . '.' . $this->algorithm);
144
        $fos->write(sprintf('%s  %s', $hashValue, basename($this->file)));
145
    }
146
147
    /**
148
     * checks file attribute
149
     *
150
     * @throws BuildException
151
     */
152
    private function checkFile(): void
153
    {
154
        // check File
155
        if ($this->file === null || $this->file === '') {
156
            throw new BuildException('[FileHash] You must specify an input file.', $this->file);
0 ignored issues
show
Bug introduced by
$this->file of type void is incompatible with the type Exception|Phing\Parser\Location|null expected by parameter $p2 of Phing\Exception\BuildException::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
            throw new BuildException('[FileHash] You must specify an input file.', /** @scrutinizer ignore-type */ $this->file);
Loading history...
157
        }
158
159
        if (!is_readable($this->file)) {
160
            throw new BuildException(
161
                sprintf(
162
                    '[FileHash] Input file does not exist or is not readable: %s',
163
                    $this->file
164
                )
165
            );
166
        }
167
    }
168
169
    /**
170
     * checks property attribute
171
     *
172
     * @throws BuildException
173
     */
174
    private function checkPropertyName(): void
175
    {
176
        if (null === $this->propertyName || $this->propertyName === '') {
177
            throw new BuildException('Property name for publishing hashvalue is not set');
178
        }
179
    }
180
}
181