SerializeException::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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