Passed
Push — master ( 28a3ce...b9357e )
by Siad
05:23
created

HashfileAlgorithm::getValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
crap 3.3332
rs 10
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
 * Class ChecksumAlgorithm
22
 */
23
class HashfileAlgorithm implements Algorithm
24
{
25
    /**
26
     * Checksum algorithm to be used.
27
     */
28
    private $algorithm = 'md5';
29
30
    /**
31
     * Specifies the algorithm to be used to compute the checksum.
32
     * Defaults to "CRC". Other popular algorithms like "ADLER" may be used as well.
33
     * @param string $algorithm the digest algorithm to use
34
     */
35 1
    public function setAlgorithm(string $algorithm): void
36
    {
37 1
        $this->algorithm = strtolower($algorithm);
38 1
    }
39
40
    /**
41
     * This algorithm supports `hash_algos()`.
42
     * @return bool <i>true</i> if all is ok, otherwise <i>false</i>.
43
     */
44 1
    public function isValid(): bool
45
    {
46 1
        return in_array($this->algorithm, hash_algos(), true);
47
    }
48
49
    /**
50
     * Computes a value for a file content with the specified checksum algorithm.
51
     * @param PhingFile $file File object for which the value should be evaluated.
52
     * @return string|null The value for that file
53
     */
54 1
    public function getValue(PhingFile $file): ?string
55
    {
56 1
        if (!$this->isValid()) {
57
            throw new BuildException('Wrong hash algorithm.');
58
        }
59
60 1
        if ($file->canRead()) {
61 1
            return hash_file($this->algorithm, $file->getAbsolutePath());
62
        }
63
        return null;
64
    }
65
66
    /**
67
     * @return string some information about this algorithm.
68
     */
69 1
    public function __toString(): string
70
    {
71 1
        return sprintf('<%s:algorithm=%s>', __CLASS__, $this->algorithm);
72
    }
73
}
74