setDerivationIterations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 9.9
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * Trait implementation of the derivation control over the internal number of iterations for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use CryptoManana\Core\Interfaces\MessageDigestion\DerivationIterationControlInterface as IterationControlSpecification;
10
11
/**
12
 * Trait DerivationIterationControlTrait - Reusable implementation of `DerivationIterationControlInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\DerivationIterationControlInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageDigestion
17
 *
18
 * @property int $numberOfIterations The derivation internal iteration count property storage.
19
 *
20
 * @mixin IterationControlSpecification
21
 */
22
trait DerivationIterationControlTrait
23
{
24
    /**
25
     * Setter for the derivation internal iteration count property.
26
     *
27
     * @param int $numberOfIterations The number of internal iterations to perform.
28
     *
29
     * @return $this The hash algorithm object.
30
     * @throws \Exception Validation errors.
31
     */
32 30
    public function setDerivationIterations($numberOfIterations)
33
    {
34 30
        $numberOfIterations = filter_var(
35 30
            $numberOfIterations,
36 30
            FILTER_VALIDATE_INT,
37 30
            [
38 30
                "options" => [
39 30
                    "min_range" => 1,
40 30
                    "max_range" => PHP_INT_MAX,
41 30
                ],
42 30
            ]
43 30
        );
44
45 30
        if ($numberOfIterations === false) {
46 15
            throw new \InvalidArgumentException(
47 15
                'The number of internal iterations must be a valid integer bigger than 0.'
48 15
            );
49
        }
50
51 15
        $this->numberOfIterations = $numberOfIterations;
52
53 15
        return $this;
54
    }
55
56
    /**
57
     * Getter for the derivation internal iteration count property.
58
     *
59
     * @return int The number of internal iterations to perform.
60
     */
61 15
    public function getDerivationIterations()
62
    {
63 15
        return $this->numberOfIterations;
64
    }
65
}
66