ObjectHashingTrait::hashObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 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