Completed
Push — master ( e70e28...7f2cdc )
by Tony Karavasilev (Тони
06:07
created

DigestionFormatsTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 1
b 0
f 0
dl 0
loc 69
ccs 28
cts 28
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A changeOutputFormat() 0 20 5
A setDigestFormat() 0 20 2
A getDigestFormat() 0 3 1
1
<?php
2
3
/**
4
 * Trait implementation of the hashing output formats for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface as DigestionFormatsSpecification;
10
11
/**
12
 * Trait DigestionFormatsTrait - Reusable implementation of `DigestionFormatsInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageDigestion
17
 *
18
 * @property int $digestFormat The digest output format property storage.
19
 *
20
 * @mixin DigestionFormatsSpecification
21
 */
22
trait DigestionFormatsTrait
23
{
24
    /**
25
     * Internal method for converting the digest's output format representation via the chosen format.
26
     *
27
     * @param string $digest The output digest.
28
     *
29
     * @return string The input data with proper salting.
30
     */
31 176
    protected function changeOutputFormat($digest)
32
    {
33 176
        switch ($this->getDigestFormat()) {
34 176
            case self::DIGEST_OUTPUT_HEX_LOWER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DIGEST_OUTPUT_HEX_LOWER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35 66
                break; // Comes already in lower, can be skipped
36 132
            case self::DIGEST_OUTPUT_HEX_UPPER:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...DIGEST_OUTPUT_HEX_UPPER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
37 110
                $digest = strtoupper($digest);
38 110
                break;
39 44
            case self::DIGEST_OUTPUT_BASE_64:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...::DIGEST_OUTPUT_BASE_64 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
40 22
                $digest = base64_encode(pack('H*', $digest));
41 22
                break;
42 44
            case self::DIGEST_OUTPUT_BASE_64_URL:
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...GEST_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
43 22
                $digest = base64_encode(pack('H*', $digest));
44 22
                $digest = str_replace(['+', '/', '='], ['-', '_', ''], $digest);
45 22
                break;
46
            default: // case self::DIGEST_OUTPUT_RAW:
47 44
                break;
48
        }
49
50 176
        return $digest;
51
    }
52
53
    /**
54
     * Setter for the digest format code property.
55
     *
56
     * @param int $digestFormat The digest format code.
57
     *
58
     * @return $this The hash algorithm object.
59
     * @throw \Exception Validation errors.
60
     */
61 110
    public function setDigestFormat($digestFormat)
62
    {
63 110
        $digestFormat = filter_var(
64 110
            $digestFormat,
65 110
            FILTER_VALIDATE_INT,
66
            [
67
                "options" => [
68 110
                    "min_range" => self::DIGEST_OUTPUT_RAW,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...rait::DIGEST_OUTPUT_RAW was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
69 110
                    "max_range" => self::DIGEST_OUTPUT_BASE_64_URL,
1 ignored issue
show
Bug introduced by
The constant CryptoManana\Core\Traits...GEST_OUTPUT_BASE_64_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
                ],
71
            ]
72
        );
73
74 110
        if ($digestFormat === false) {
75 22
            throw new \InvalidArgumentException('Digest output format mode must be an integer between -1 and 3.');
76
        }
77
78 88
        $this->digestFormat = $digestFormat;
79
80 88
        return $this;
81
    }
82
83
    /**
84
     * Getter for the digest format code property.
85
     *
86
     * @return int The digest format code.
87
     */
88 176
    public function getDigestFormat()
89
    {
90 176
        return $this->digestFormat;
91
    }
92
}
93