ObjectHashingTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 6
c 1
b 0
f 0
dl 0
loc 29
ccs 5
cts 5
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A hashObject() 0 9 2
1
<?php
2
3
/**
4
 * Trait implementation of object hashing for digestion algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageDigestion;
8
9
use CryptoManana\Core\Abstractions\MessageDigestion\AbstractHashAlgorithm as AnyDerivedHashAlgorithm;
10
use CryptoManana\Core\Interfaces\MessageDigestion\ObjectHashingInterface as ObjectHashingSpecification;
11
12
/**
13
 * Trait ObjectHashingTrait - Reusable implementation of `ObjectHashingInterface`.
14
 *
15
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\ObjectHashingInterface The abstract specification.
16
 *
17
 * @package CryptoManana\Core\Traits\MessageDigestion
18
 *
19
 * @mixin ObjectHashingSpecification
20
 * @mixin AnyDerivedHashAlgorithm
21
 */
22
trait ObjectHashingTrait
23
{
24
    /**
25
     * Calculates a hash value for the given data.
26
     *
27
     * @param string $data The input string.
28
     *
29
     * @return string The digest.
30
     * @throws \Exception Validation errors.
31
     */
32
    abstract public function hashData($data);
33
34
    /**
35
     * Calculates a hash value for the serialized value of the given object.
36
     *
37
     * @param object|\stdClass $object The object for hashing.
38
     *
39
     * @return string The digest.
40
     * @throws \Exception Validation errors.
41
     */
42 60
    public function hashObject($object)
43
    {
44 60
        if (is_object($object)) {
45 30
            $object = serialize($object);
46
        } else {
47 30
            throw new \InvalidArgumentException('The data for hashing must be an object instance.');
48
        }
49
50 30
        return $this->hashData($object);
51
    }
52
}
53