RemoteExceptionWrapper::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * AppserverIo\RemoteMethodInvocation\RemoteExceptionWrapper
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/rmi
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\RemoteMethodInvocation;
22
23
/**
24
 * A wrapper to make exceptions serializable.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2015 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/rmi
30
 * @link      http://www.appserver.io
31
 */
32
class RemoteExceptionWrapper implements \Serializable
33
{
34
35
    /**
36
     * The original exception type.
37
     *
38
     * @var string
39
     */
40
    protected $type = '\Exception';
41
42
    /**
43
     * The exception code.
44
     *
45
     * @var integer
46
     */
47
    protected $code = 0;
48
49
    /**
50
     * The filename where the exception was created.
51
     *
52
     * @var string
53
     */
54
    protected $file = '';
55
56
    /**
57
     * The line where the exception was created.
58
     *
59
     * @var integer
60
     */
61
    protected $line = 0;
62
63
    /**
64
     * The original exception stack trace.
65
     *
66
     * @var string
67
     */
68
    protected $trace = '';
69
70
    /**
71
     * The exception message.
72
     *
73
     * @var string
74
     */
75
    protected $message = '';
76
77
    /**
78
     * Initializes the wrapper with the data from the passed exception.
79
     *
80
     * @param \Exception $e The exception we want to wrap
81
     */
82
    public function __construct(\Exception $e)
83
    {
84
        // initialize the wrapper with the exception's data
85
        $this->type = get_class($e);
86
        $this->code = $e->getCode();
87
        $this->file = $e->getFile();
88
        $this->line = $e->getLine();
89
        $this->trace = $e->getTraceAsString();
90
        $this->message = $e->__toString();
91
    }
92
93
    /**
94
     * Factory method to create a new wrapper instance from the
95
     * passed exception data.
96
     *
97
     * @param \Exception $e The exception to wrap
98
     *
99
     * @return \AppserverIo\RemoteMethodInvocation\RemoteExceptionWrapper The wrapper instance
100
     */
101
    public function factory(\Exception $e)
102
    {
103
        return new RemoteExceptionWrapper($e);
104
    }
105
106
    /**
107
     * This method has to be called to serialize the String.
108
     *
109
     * @return string Returns a serialized version of the String
110
     * @see \Serializable::serialize()
111
     */
112
    public function serialize()
113
    {
114
        return serialize(get_object_vars($this));
115
    }
116
117
    /**
118
     * This method unserializes the passed string and initializes the String
119
     * itself with the data.
120
     *
121
     * @param string $data Holds the data of the instance as serialized string
122
     *
123
     * @return void
124
     * @see \Serializable::unserialize($data)
125
     */
126
    public function unserialize($data)
127
    {
128
        foreach (unserialize($data) as $propertyName => $propertyValue) {
129
            $this->$propertyName = $propertyValue;
130
        }
131
    }
132
133
    /**
134
     * Returns a new instance of the original exception
135
     * and it's data.
136
     *
137
     * @return \Exception An instance of the original exception
138
     */
139
    public function toException()
140
    {
141
        return new $this->type($this->message, $this->code);
142
    }
143
}
144