Completed
Pull Request — master (#2)
by Pol
01:18
created

AbstractExceptionObject::hash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.8333
c 0
b 0
f 0
nc 1
cc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\valuewrapper\Object;
6
7
/**
8
 * Class AbstractExceptionObject
9
 */
10
abstract class AbstractExceptionObject extends ObjectValue
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 6
    public function hash(): string
16
    {
17
        /** @var \TypeError $value */
18 6
        $value = $this->get();
19
20
        $data = [
21 6
            'message' => $value->getMessage(),
22 6
            'code' => $value->getCode(),
23 6
            'class' => get_class($this->get()),
24
        ];
25
26 6
        return $this->doHash(implode('', $data));
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 3
    public function serialize()
33
    {
34
        /** @var \Exception $exception */
35 3
        $exception = $this->get();
36
37
        $data = [
38 3
            'message' => $exception->getMessage(),
39 3
            'code' => $exception->getCode(),
40
        ];
41
42 3
        return \serialize([
43 3
            'value' => $data,
44
        ]);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function unserialize($serialized)
51
    {
52
        $unserialize = \unserialize($serialized);
53
54
        $this->set(
55
            (new \Exception($unserialize['value']['message'], $unserialize['value']['code']))
56
        );
57
    }
58
}
59