Completed
Pull Request — master (#2)
by Pol
02:24
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
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\valuewrapper\Object\Exception;
6
7
use drupol\valuewrapper\Object\ObjectValue;
8
9
/**
10
 * Class AbstractExceptionObject
11
 */
12
abstract class AbstractExceptionObject extends ObjectValue
13
{
14
    /**
15
     * Throwable constructor.
16
     *
17
     * @param \Throwable $value
18
     */
19 12
    public function __construct(\Throwable $value)
20
    {
21 12
        parent::__construct($value);
22 12
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 6
    public function hash(): string
28
    {
29
        /** @var \TypeError $value */
30 6
        $value = $this->get();
31
32
        $data = [
33 6
            'message' => $value->getMessage(),
34 6
            'code' => $value->getCode(),
35 6
            'class' => $this->class(),
36
        ];
37
38 6
        return $this->doHash(implode('', $data));
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 3
    public function serialize()
45
    {
46 3
        return \serialize([
47
            'value' => [
48 3
                'message' => $this->get()->getMessage(),
49 3
                'code' => $this->get()->getCode(),
50 3
                'class' => $this->class(),
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function unserialize($serialized)
59
    {
60 3
        $unserialize = \unserialize($serialized);
61
62 3
        $reflection = new \ReflectionClass($unserialize['value']['class']);
63
64 3
        $this->set(
65 3
            ($reflection->newInstanceArgs([$unserialize['value']['message'], $unserialize['value']['code']]))
66
        );
67 3
    }
68
}
69