RemoteMethodProtocol   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 105
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareHeaderInvoke() 0 4 1
A prepareHeaderResult() 0 4 1
A prepareHeader() 0 10 1
A pack() 0 4 1
A unpack() 0 4 1
1
<?php
2
3
/**
4
 * AppserverIo\RemoteMethodInvocation\RemoteMethodProtocol
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
 * This is a parser for a native persistence container remote method call.
25
 *
26
 * A Remote method call must have the following format:
27
 *
28
 * <METHOD> <CONTENT-LENGTH> <PROTOCOL>/<VERSION>\r\n
29
 * <CONTENT>\r\n
30
 *
31
 * for example:
32
 *
33
 * INVOKE 12 RMC/1.0\r\n
34
 * czoxOiIxIjs=\r\n
35
 *
36
 * @author    Tim Wagner <[email protected]>
37
 * @copyright 2015 TechDivision GmbH <[email protected]>
38
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
39
 * @link      https://github.com/appserver-io/rmi
40
 * @link      http://www.appserver.io
41
 */
42
class RemoteMethodProtocol
43
{
44
45
    /**
46
     * This is the line ending we use.
47
     *
48
     * @var string EOL
49
     */
50
    const EOL = "\r\n";
51
52
    /**
53
     * Protocol identifier.
54
     *
55
     * @var string PROTOCOL
56
     */
57
    const PROTOCOL = 'RMC';
58
59
    /**
60
     * Protocol version.
61
     *
62
     * @var string VERSION
63
     */
64
    const VERSION = '1.0';
65
66
    /**
67
     * Protocol method that signals a method remote method call.
68
     *
69
     * @var string REMOTE_METHOD_INVOKE
70
     */
71
    const REMOTE_METHOD_INVOKE = 'INVOKE';
72
73
    /**
74
     * Protocol method that signals the result of a remote method call.
75
     *
76
     * @var string REMOTE_METHOD_RESULT
77
     */
78
    const REMOTE_METHOD_RESULT = 'RESULT';
79
80
    /**
81
     * Prepares the header line for a remote method invocation request.
82
     *
83
     * @param string $string The packed remote method instance
84
     *
85
     * @return string The remote method invocation header for the passed remote method instance
86
     */
87
    public static function prepareHeaderInvoke($string)
88
    {
89
        return RemoteMethodProtocol::prepareHeader(RemoteMethodProtocol::REMOTE_METHOD_INVOKE, $string);
90
    }
91
92
    /**
93
     * Prepares the header line for a remote method result request.
94
     *
95
     * @param string $string The packed remote method instance
96
     *
97
     * @return string The remote method result header for the passed remote method instance
98
     */
99
    public static function prepareHeaderResult($string)
100
    {
101
        return RemoteMethodProtocol::prepareHeader(RemoteMethodProtocol::REMOTE_METHOD_RESULT, $string);
102
    }
103
104
    /**
105
     * Prepares the header line for the passed remote method.
106
     *
107
     * @param string $method The remote method to prepare the head for
108
     * @param string $string The packed remote method instance
109
     *
110
     * @return string The remote method header for the passed method
111
     */
112
    protected static function prepareHeader($method, $string)
113
    {
114
        // prepare the header elements
115
        $protocol = RemoteMethodProtocol::PROTOCOL;
116
        $version = RemoteMethodProtocol::VERSION;
117
        $contentLength = strlen($string);
118
119
        // concatenate the header string
120
        return "$method $contentLength $protocol/$version" . RemoteMethodProtocol::EOL;
121
    }
122
123
    /**
124
     * Packs the passed instance.
125
     *
126
     * @param object $instance The instance to be packed
127
     *
128
     * @return string The packed instance
129
     */
130
    public static function pack($instance)
131
    {
132
        return base64_encode(serialize($instance));
133
    }
134
135
    /**
136
     * Unpacks the passed instance.
137
     *
138
     * @param string $string The packed object instance.
139
     *
140
     * @return object The unpacke object instance
141
     */
142
    public static function unpack($string)
143
    {
144
        return unserialize(base64_decode($string));
145
    }
146
}
147