AbstractExceptionObject   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 17
c 1
b 0
f 0
dl 0
loc 54
ccs 19
cts 19
cp 1
rs 10

4 Methods

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