Completed
Push — master ( ba89b0...71c4cf )
by Tony Karavasilev (Тони
17:29
created

FileHashingTrait::validateFileNamePath()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.2222
cc 6
nc 4
nop 1
crap 6
1
<?php
2
3
/**
4
 * Trait implementation of file hashing for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use \CryptoManana\Core\Interfaces\MessageDigestion\FileHashingInterface as FileHashingSpecification;
10
use \CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as AnyDerivedHashAlgorithm;
11
use \CryptoManana\Core\Traits\CommonValidations\FileNameValidationTrait as ValidateFileNames;
12
13
/**
14
 * Trait FileHashingTrait - Reusable implementation of `FileHashingInterface`.
15
 *
16
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\FileHashingInterface The abstract specification.
17
 *
18
 * @package CryptoManana\Core\Traits\MessageDigestion
19
 *
20
 * @mixin FileHashingSpecification
21
 * @mixin AnyDerivedHashAlgorithm
22
 * @mixin ValidateFileNames
23
 */
24
trait FileHashingTrait
25
{
26
    /**
27
     * File name and path validations.
28
     *
29
     * {@internal Reusable implementation of the common file name validation. }}
30
     */
31
    use ValidateFileNames;
32
33
    /**
34
     * Internal method for checking if native file hashing should be used by force.
35
     *
36
     * @return bool Is native hashing needed for the current salting mode.
37
     */
38 44
    protected function isFileSaltingForcingNativeHashing()
39
    {
40
        return (
41
            (
42
                // If there is an non-empty salt string set and salting is enabled
43 44
                $this->salt !== '' &&
44 44
                $this->saltingMode !== self::SALTING_MODE_NONE
45
            ) || (
46
                // If there is an empty salt string set and the salting mode duplicates/manipulates the input
47 44
                $this->salt === '' &&
48 44
                in_array($this->saltingMode, [self::SALTING_MODE_INFIX_SALT, self::SALTING_MODE_PALINDROME_MIRRORING])
49
            )
50
        );
51
    }
52
53
    /**
54
     * Calculates a hash value for the content of the given filename and location.
55
     *
56
     * @param string $filename The full path and name of the file for hashing.
57
     *
58
     * @return string The digest.
59
     * @throws \Exception Validation errors.
60
     */
61
    abstract public function hashFile($filename);
62
}
63