AlgorithmicCostTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 42
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlgorithmicCost() 0 3 1
A setAlgorithmicCost() 0 22 2
1
<?php
2
3
/**
4
 * Trait implementation of the total algorithmic cost tuning per one computation for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use CryptoManana\Core\Interfaces\MessageDigestion\AlgorithmicCostInterface as AlgorithmicCostSpecification;
10
11
/**
12
 * Trait AlgorithmicCostTrait - Reusable implementation of `AlgorithmicCostInterface`.
13
 *
14
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\AlgorithmicCostInterface The abstract specification.
15
 *
16
 * @package CryptoManana\Core\Traits\MessageDigestion
17
 *
18
 * @property int $computationalCost The digestion internal computational cost property storage.
19
 *
20
 * @mixin AlgorithmicCostSpecification
21
 */
22
trait AlgorithmicCostTrait
23
{
24
    /**
25
     * Setter for the computational cost property.
26
     *
27
     * @param int $cost The algorithmic cost.
28
     *
29
     * @return $this The hash algorithm object.
30
     * @throws \Exception Validation errors.
31
     */
32 17
    public function setAlgorithmicCost($cost)
33
    {
34 17
        $cost = filter_var(
35 17
            $cost,
36 17
            FILTER_VALIDATE_INT,
37 17
            [
38 17
                "options" => [
39 17
                    "min_range" => static::MINIMUM_ALGORITHMIC_COST,
40 17
                    "max_range" => static::MAXIMUM_ALGORITHMIC_COST,
41 17
                ],
42 17
            ]
43 17
        );
44
45 17
        if ($cost === false) {
46 1
            throw new \InvalidArgumentException(
47 1
                'The number of internal iterations must be a valid integer bigger than 0.'
48 1
            );
49
        }
50
51 17
        $this->computationalCost = $cost;
52
53 17
        return $this;
54
    }
55
56
    /**
57
     * Getter for the computational cost property.
58
     *
59
     * @return int The algorithmic cost.
60
     */
61 1
    public function getAlgorithmicCost()
62
    {
63 1
        return $this->computationalCost;
64
    }
65
}
66