RemoteProxy::__invoke()   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 2
crap 2
1
<?php
2
3
/**
4
 * \AppserverIo\RemoteMethodInvocation\RemoteProxy
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
 * @author    Bernhard Wick <[email protected]>
16
 * @copyright 2015 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/appserver-io/rmi
19
 * @link      http://www.appserver.io
20
 */
21
22
namespace AppserverIo\RemoteMethodInvocation;
23
24
/**
25
 * The proxy is used to create a new remote object of the
26
 * class with the requested name.
27
 *
28
 * namespace AppserverIo\Example;
29
 *
30
 * use AppserverIo\RemoteMethodInvocation\RemoteConnectionFactory;
31
 *
32
 * $connection = RemoteConnectionFactory::createContextConnection();
33
 * $session = $connection->createContextSession();
34
 * $initialContext = $session->createInitialContext();
35
 *
36
 * $processor = $initialContext->lookup('Some\ProxyClass');
37
 *
38
 * @author    Tim Wagner <[email protected]>
39
 * @author    Bernhard Wick <[email protected]>
40
 * @copyright 2015 TechDivision GmbH <[email protected]>
41
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
42
 * @link      https://github.com/appserver-io/rmi
43
 * @link      http://www.appserver.io
44
 */
45
class RemoteProxy implements RemoteObjectInterface
46
{
47
48
    /**
49
     * Holds the ContextSession for this proxy.
50
     *
51
     * @var \AppserverIo\RemoteMethodInvocation\SessionInterface
52
     */
53
    protected $session = null;
54
55
    /**
56
     * The class name to proxy.
57
     *
58
     * @var string $className
59
     */
60
    protected $className = null;
61
62
    /**
63
     * Initializes the proxy with the class name to proxy.
64
     *
65
     * @param mixed $className The name of the class to create the proxy for
66
     */
67
    public function __construct($className = 'AppserverIo\Appserver\Core\InitialContext')
68
    {
69
        $this->className = $className;
70
    }
71
72
    /**
73
     * The name of the original object.
74
     *
75
     * @return string The name of the original object
76
     * @see \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface::__getClassName()
77
     */
78
    public function __getClassName()
79
    {
80
        return $this->className;
81
    }
82
83
    /**
84
     * Sets the session with the connection instance.
85
     *
86
     * @param \AppserverIo\RemoteMethodInvocation\SessionInterface $session The session instance to use
87
     *
88
     * @return \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface The instance itself
89
     */
90
    public function __setSession(SessionInterface $session)
91
    {
92
        $this->session = $session;
93
        return $this;
94
    }
95
96
    /**
97
     * Returns the session instance.
98
     *
99
     * @return \AppserverIo\RemoteMethodInvocation\SessionInterface The session instance
100
     * @see \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface::__getSession()
101
     */
102
    public function __getSession()
103
    {
104
        return $this->session;
105
    }
106
107
    /**
108
     * Invokes the remote execution of the passed remote method.
109
     *
110
     * @param string $method The remote method to call
111
     * @param array  $params The parameters for the method call
112
     *
113
     * @return mixed The result of the remote method call
114
     */
115
    public function __call($method, $params)
116
    {
117
        $methodCall = new RemoteMethodCall($this->__getClassName(), $method, $this->__getSession()->getSessionId());
118
        foreach ($params as $key => $value) {
119
            $methodCall->addParameter($key, $value);
120
        }
121
        return $this->__invoke($methodCall, $this->__getSession());
122
    }
123
124
    /**
125
     * Invokes the remote execution of the passed remote method.
126
     *
127
     * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $methodCall The remote method call instance
128
     * @param \AppserverIo\RemoteMethodInvocation\SessionInterface      $session    The session with the connection instance to use
129
     *
130
     * @return mixed The result of the remote method call
131
     */
132
    public function __invoke(RemoteMethodInterface $methodCall, SessionInterface $session)
133
    {
134
        return $this->__setSession($session)->__getSession()->send($methodCall);
135
    }
136
137
    /**
138
     * Factory method to create a new instance of the requested proxy implementation.
139
     *
140
     * @param string $className The name of the class to create the proxy for
141
     *
142
     * @return \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface The proxy instance
143
     */
144
    public static function __create($className)
145
    {
146
        return new RemoteProxy($className);
147
    }
148
}
149