Passed
Push — main ( 9f1665...bf42c8 )
by Teodoro
02:24
created

SerializeException::exception()   A

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
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tleckie\Async;
4
5
use Serializable;
6
use Throwable;
7
8
/**
9
 * Class SerializeException
10
 *
11
 * @package Tleckie\Async
12
 * @author  Teodoro Leckie Westberg <[email protected]>
13
 */
14
class SerializeException implements Serializable
15
{
16
    /** @var array */
17
    private array $data = [];
18
19
    /** @var Throwable */
20
    private Throwable $exception;
21
22
    /**
23
     * SerializeException constructor.
24
     *
25
     * @param Throwable $exception
26
     */
27
    public function __construct(Throwable $exception)
28
    {
29
        $this->data[] = get_class($exception);
30
        $this->data[] = sprintf("%s %s", $exception->getMessage(), $exception->getTraceAsString());
31
        $this->data[] = $exception->getCode();
32
        $this->data[] = $exception->getPrevious();
33
    }
34
35
    /**
36
     * @return string
37
     */
38
    public function serialize(): string
39
    {
40
        return \serialize($this->data);
41
    }
42
43
    /**
44
     * @return Throwable
45
     */
46
    public function exception(): Throwable
47
    {
48
        return $this->exception;
49
    }
50
51
    /**
52
     * @param string $serialized
53
     * @return Throwable
54
     */
55
    public function unserialize($serialized)
56
    {
57
        $data = \unserialize($serialized);
58
59
        [$className, $message, $code, $previous] = $data;
60
61
        $this->exception = new $className(
62
            $message,
63
            $code,
64
            $previous,
65
        );
66
    }
67
}
68