Completed
Push — master ( 9f5552...6c4dfd )
by Siad
15:59
created

FileHashTask::main()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7.8294

Importance

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

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