Completed
Push — master ( 8055c8...3e2656 )
by Tony Karavasilev (Тони
16:45
created

ObjectEncryptionTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 63
ccs 0
cts 20
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encryptObject() 0 9 2
A decryptObject() 0 13 3
1
<?php
2
3
/**
4
 * Trait implementation of object encryption/decryption for symmetric encryption algorithms.
5
 */
6
7
namespace CryptoManana\Core\Traits\MessageEncryption;
8
9
use \CryptoManana\Core\Interfaces\MessageEncryption\ObjectEncryptionInterface as ObjectEncryptionSpecification;
10
use \CryptoManana\Core\Abstractions\MessageEncryption\AbstractSymmetricEncryptionAlgorithm as AnyEncryptionAlgorithm;
11
use \CryptoManana\Core\StringBuilder as StringBuilder;
12
13
/**
14
 * Trait ObjectEncryptionTrait - Reusable implementation of `ObjectEncryptionInterface`.
15
 *
16
 * @see \CryptoManana\Core\Interfaces\MessageDigestion\ObjectEncryptionInterface The abstract specification.
17
 *
18
 * @package CryptoManana\Core\Traits\MessageEncryption
19
 *
20
 * @mixin ObjectEncryptionSpecification
21
 * @mixin AnyEncryptionAlgorithm
22
 */
23
trait ObjectEncryptionTrait
24
{
25
    /**
26
     * Encrypts the serialized value of the given object.
27
     *
28
     * @param object|\stdClass $object The object for hashing.
29
     *
30
     * @return string The encrypted serialized object as a string.
31
     * @throws \Exception Validation errors.
32
     */
33
    public function encryptObject($object)
34
    {
35
        if (is_object($object)) {
36
            $object = serialize($object);
37
        } else {
38
            throw new \InvalidArgumentException('The data for encryption must be an object instance.');
39
        }
40
41
        return $this->encryptData($object);
42
    }
43
44
    /**
45
     * Decrypts the encrypted value of the given object.
46
     *
47
     * @param string $cipherData The encrypted serialized object as a string.
48
     *
49
     * @return object|\stdClass The decrypted and unserialized object.
50
     * @throws \Exception Validation errors.
51
     */
52
    public function decryptObject($cipherData)
53
    {
54
        if (!is_string($cipherData)) {
1 ignored issue
show
introduced by
The condition is_string($cipherData) is always true.
Loading history...
55
            throw new \InvalidArgumentException('The data for decryption must be a string or a binary string.');
56
        }
57
58
        $object = unserialize($this->decryptData($cipherData));
59
60
        if (!is_object($object)) {
61
            throw new \InvalidArgumentException('The decrypted data must be an object instance.');
62
        }
63
64
        return $object;
65
    }
66
67
    /**
68
     * Encrypts the given plain data.
69
     *
70
     * @param string $plainData The plain input string.
71
     *
72
     * @return string The cipher/encrypted data.
73
     * @throws \Exception Validation errors.
74
     */
75
    abstract public function encryptData($plainData);
76
77
    /**
78
     * Decrypts the given cipher data.
79
     *
80
     * @param string $cipherData The encrypted input string.
81
     *
82
     * @return string The decrypted/plain data.
83
     * @throws \Exception Validation errors.
84
     */
85
    abstract public function decryptData($cipherData);
86
}
87