Completed
Push — master ( df57bf...0736f1 )
by Pol
09:33
created

AbstractExceptionObject   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 57
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hash() 0 13 1
A serialize() 0 10 1
A unserialize() 0 10 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 28
    public function __construct(\Throwable $value)
20
    {
21 28
        parent::__construct($value);
22 28
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 14
    public function hash(): string
28
    {
29
        /** @var \TypeError $value */
30 14
        $value = $this->value();
31
32
        $data = [
33 14
            'message' => $value->getMessage(),
34 14
            'code' => $value->getCode(),
35 14
            'class' => $this->class(),
36
        ];
37
38 14
        return $this->doHash($this->type() . $this->class() . implode('', $data));
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 7
    public function serialize()
45
    {
46 7
        return \serialize([
47
            'value' => [
48 7
                'message' => $this->value()->getMessage(),
49 7
                'code' => $this->value()->getCode(),
50 7
                'class' => $this->class(),
51
            ],
52
        ]);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 7
    public function unserialize($serialized)
59
    {
60 7
        $unserialize = \unserialize($serialized);
61
62 7
        $reflection = new \ReflectionClass($unserialize['value']['class']);
63
64 7
        $this->set(
65 7
            ($reflection->newInstanceArgs([$unserialize['value']['message'], $unserialize['value']['code']]))
66
        );
67 7
    }
68
}
69