Completed
Push — master ( c76e6f...8dadf4 )
by Tim
11:44 queued 01:41
created

ContextSession::cleanUp()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 8.5906
cc 5
eloc 8
nc 5
nop 0
1
<?php
2
3
/**
4
 * AppserverIo\RemoteMethodInvocation\RemoteMethod
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
use AppserverIo\Collections\HashMap;
25
use AppserverIo\Psr\Servlet\SessionUtils;
26
use AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface;
27
28
/**
29
 * The interface for the remote connection.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @author    Bernhard Wick <[email protected]>
33
 * @copyright 2015 TechDivision GmbH <[email protected]>
34
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
35
 * @link      https://github.com/appserver-io/rmi
36
 * @link      http://www.appserver.io
37
 */
38
class ContextSession extends HashMap implements SessionInterface
39
{
40
41
    /**
42
     * The connection instances.
43
     *
44
     * @var array
45
     */
46
    protected $connections = null;
47
48
    /**
49
     * The session ID used for the connection.
50
     *
51
     * @var string
52
     */
53
    protected $sessionId = null;
54
55
    /**
56
     * The servlet request to load the session ID used for the connection.
57
     *
58
     * @var string
59
     */
60
    protected $servletRequest = null;
61
62
    /**
63
     * Initializes the session with the connection.
64
     *
65
     * @param \AppserverIo\RemoteMethodInvocation\ConnectionInterface $connection The connection for the session
66
     */
67
    public function __construct(ConnectionInterface $connection)
68
    {
69
70
        // parent constructor to ensure property preset
71
        parent::__construct(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
73
        // initialize the array for the collections
74
        $this->connections = array();
75
76
        // add the passed connection
77
        $this->addConnection($connection);
78
    }
79
80
    /**
81
     * Re-Attaches the beans bound to this session to the container.
82
     *
83
     * @return void
84
     */
85
    public function __destruct()
86
    {
87
        $this->cleanUp();
88
    }
89
90
    /**
91
     * Clean-Up the session context by re-attaching the
92
     * session beans to the container.
93
     *
94
     * @return void
95
     */
96
    protected function cleanUp()
97
    {
98
99
        // query whether we've beans that has to be re-attached to the container or not
100
        if ($this->size() > 0) {
101
            // iterate over all connections to query if the bean has to be re-attached
102
            foreach ($this->getConnections() as $connection) {
103
                // query whether we've local context connection or not
104
                if ($application = $connection->getApplication()) {
105
                    // load the bean manager instance from the application
106
                    $beanManager = $application->search('BeanContextInterface');
107
108
                    // load the session-ID
109
                    $sessionId = $this->getSessionId();
110
111
                    // attach all beans of this session
112
                    foreach ($this->items as $instance) {
113
                        $beanManager->attach($instance, $sessionId);
114
                    }
115
                }
116
            }
117
        }
118
    }
119
120
    /**
121
     * Injects the servlet request to load the session ID, for the remote method call, from.
122
     *
123
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The servlet request instance to inject
124
     *
125
     * @return void
126
     */
127
    public function injectServletRequest(HttpServletRequestInterface $servletRequest)
128
    {
129
        $this->servletRequest = $servletRequest;
0 ignored issues
show
Documentation Bug introduced by
It seems like $servletRequest of type object<AppserverIo\Psr\S...ervletRequestInterface> is incompatible with the declared type string of property $servletRequest.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
130
    }
131
132
    /**
133
     * Returns the servlet request instance to load the session ID from.
134
     *
135
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface The servlet request instance
136
     */
137
    public function getServletRequest()
138
    {
139
        return $this->servletRequest;
140
    }
141
142
    /**
143
     * Add's the passed connection to the session's connection collection.
144
     *
145
     * @param \AppserverIo\RemoteMethodInvocation\ConnectionInterface $connection The connection instance to add
146
     *
147
     * @return void
148
     */
149
    public function addConnection(ConnectionInterface $connection)
150
    {
151
        $this->connections[] = $connection;
152
    }
153
154
    /**
155
     * Returns the collection with the session's connections.
156
     *
157
     * @return array The collection with the session's connections
158
     */
159
    public function getConnections()
160
    {
161
        return $this->connections;
162
    }
163
164
    /**
165
     * Returns the connection instance.
166
     *
167
     * @param integer $key The key of the connection to return
168
     *
169
     * @return \AppserverIo\RemoteMethodInvocation\ConnectionInterface The connection instance
170
     */
171
    public function getConnection($key = 0)
172
    {
173
        return $this->connections[$key];
174
    }
175
176
    /**
177
     * Returns the ID of the session to use for connecting to the SFSBs.
178
     *
179
     * @return string The session ID
180
     * @see \AppserverIo\RemoteMethodInvocation\SessionInterface::getSessionId()
181
     */
182
    public function getSessionId()
183
    {
184
185
        // this is necessary, because in most cases, the session will be started after the
186
        // SFSB has been injected, so we've to query for a session in method invocation
187
188
        // query whether we've a HTTP session ID in the servlet request.
189
        /** \AppserverIo\Psr\Servlet\Http\HttpSessionInterface $session */
190
        if ($this->getServletRequest() && $session = $this->getServletRequest()->getSession()) {
0 ignored issues
show
Bug introduced by
The method getSession cannot be called on $this->getServletRequest() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
191
            return $session->getId();
192
        }
193
194
        // query whether we've a session ID that has been manually set
195
        if ($this->sessionId != null) {
196
            return $this->sessionId;
197
        }
198
    }
199
200
    /**
201
     * The session ID to use.
202
     *
203
     * @param string $sessionId The session ID to use
204
     *
205
     * @return void
206
     */
207
    public function setSessionId($sessionId)
208
    {
209
        $this->sessionId = $sessionId;
210
    }
211
212
    /**
213
     * Invokes the remote method over the connection.
214
     *
215
     * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $remoteMethod The remote method call to invoke
216
     *
217
     * @return mixed the method return value
218
     * @see AppserverIo\RemoteMethodInvocation\SessionInterface::send()
219
     * @todo Refactor to replace check for 'setSession' method, e. g. check for an interface
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
220
     */
221
    public function send(RemoteMethodInterface $remoteMethod)
222
    {
223
224
        // create an array to store connection response temporarily
225
        $responses = array();
226
227
        // iterate over all connections and invoke the remote method call
228
        foreach ($this->getConnections() as $key => $connection) {
229
            // invoke the remote method on the connection
230
            $responses[$key] = $connection->send($remoteMethod);
231
232
            // check if a proxy has been returned
233
            if (method_exists($responses[$key], 'setSession')) {
234
                $responses[$key]->setSession($this);
235
            }
236
        }
237
238
        // clean-up the session context
239
        $this->cleanUp();
240
241
        // return the response of the first connection
242
        return reset($responses);
243
    }
244
245
    /**
246
     * Creates a remote initial context instance.
247
     *
248
     * @return \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface The proxy for the initial context
249
     * @see \AppserverIo\RemoteMethodInvocation\SessionInterface::createInitialContext()
250
     */
251
    public function createInitialContext()
252
    {
253
        $initialContext = new InitialContextProxy();
254
        $initialContext->__setSession($this);
255
        return $initialContext;
256
    }
257
}
258