Serialized::getTrace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tleckie\CircuitBreaker\Exception;
6
7
use Exception;
8
use Serializable;
9
use function serialize;
10
use function unserialize;
11
12
/**
13
 * Class Serialized
14
 * @package Tleckie\CircuitBreaker\Exception
15
 */
16
class Serialized implements Serializable
17
{
18
    /** @var array */
19
    protected array $data = [
20
        'message' => '',
21
        'code' => 0,
22
        'className' => '',
23
        'trace' => []
24
    ];
25
26
    /**
27
     * Serialized constructor.
28
     * @param Exception|null $exception
29
     */
30
    public function __construct(Exception $exception = null)
31
    {
32
        if ($exception) {
33
            $this
34
                ->setMessage($exception->getMessage())
35
                ->setClassName($exception::class)
36
                ->setCode($exception->getCode())
37
                ->setTrace($exception->getTrace());
38
        }
39
    }
40
41
    /**
42
     * @param array $trace
43
     * @return $this
44
     */
45
    public function setTrace(array $trace): Serialized
46
    {
47
        $this->data['trace'] = $trace;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param int $code
54
     * @return $this
55
     */
56
    public function setCode(int $code): Serialized
57
    {
58
        $this->data['code'] = $code;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $className
65
     * @return $this
66
     */
67
    public function setClassName(string $className): Serialized
68
    {
69
        $this->data['className'] = $className;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @param string $message
76
     * @return $this
77
     */
78
    public function setMessage(string $message): Serialized
79
    {
80
        $this->data['message'] = $message;
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return string|null
87
     */
88
    public function getMessage(): ?string
89
    {
90
        return $this->data['message'] ?? '';
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getCode(): int
97
    {
98
        return $this->data['code'];
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getTrace(): array
105
    {
106
        return $this->data['trace'];
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getClassName(): string
113
    {
114
        return $this->data['className'];
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function serialize()
121
    {
122
        return serialize($this->data);
123
    }
124
125
    public function unserialize($data): void
126
    {
127
        $this->data = unserialize($data);
128
    }
129
}
130