Completed
Push — master ( c80e58...5e2f2d )
by Tim
19:52 queued 09:50
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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 8
nc 5
nop 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ContextSession::getServletRequest() 0 4 1
A ContextSession::addConnection() 0 4 1
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
     * Injects the servlet request to load the session ID, for the remote method call, from.
82
     *
83
     * @param \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface $servletRequest The servlet request instance to inject
84
     *
85
     * @return void
86
     */
87
    public function injectServletRequest(HttpServletRequestInterface $servletRequest)
88
    {
89
        $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...
90
    }
91
92
    /**
93
     * Returns the servlet request instance to load the session ID from.
94
     *
95
     * @return \AppserverIo\Psr\Servlet\Http\HttpServletRequestInterface The servlet request instance
96
     */
97
    public function getServletRequest()
98
    {
99
        return $this->servletRequest;
100
    }
101
102
    /**
103
     * Add's the passed connection to the session's connection collection.
104
     *
105
     * @param \AppserverIo\RemoteMethodInvocation\ConnectionInterface $connection The connection instance to add
106
     *
107
     * @return void
108
     */
109
    public function addConnection(ConnectionInterface $connection)
110
    {
111
        $this->connections[] = $connection;
112
    }
113
114
    /**
115
     * Returns the collection with the session's connections.
116
     *
117
     * @return array The collection with the session's connections
118
     */
119
    public function getConnections()
120
    {
121
        return $this->connections;
122
    }
123
124
    /**
125
     * Returns the connection instance.
126
     *
127
     * @param integer $key The key of the connection to return
128
     *
129
     * @return \AppserverIo\RemoteMethodInvocation\ConnectionInterface The connection instance
130
     */
131
    public function getConnection($key = 0)
132
    {
133
        return $this->connections[$key];
134
    }
135
136
    /**
137
     * Returns the ID of the session to use for connecting to the SFSBs.
138
     *
139
     * @return string The session ID
140
     * @see \AppserverIo\RemoteMethodInvocation\SessionInterface::getSessionId()
141
     */
142
    public function getSessionId()
143
    {
144
145
        // this is necessary, because in most cases, the session will be started after the
146
        // SFSB has been injected, so we've to query for a session in method invocation
147
148
        // query whether we've a HTTP session ID in the servlet request.
149
        /** \AppserverIo\Psr\Servlet\Http\HttpSessionInterface $session */
150
        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...
151
            return $session->getId();
152
        }
153
154
        // query whether we've a session ID that has been manually set
155
        if ($this->sessionId != null) {
156
            return $this->sessionId;
157
        }
158
    }
159
160
    /**
161
     * The session ID to use.
162
     *
163
     * @param string $sessionId The session ID to use
164
     *
165
     * @return void
166
     */
167
    public function setSessionId($sessionId)
168
    {
169
        $this->sessionId = $sessionId;
170
    }
171
172
    /**
173
     * Invokes the remote method over the connection.
174
     *
175
     * @param \AppserverIo\RemoteMethodInvocation\RemoteMethodInterface $remoteMethod The remote method call to invoke
176
     *
177
     * @return mixed the method return value
178
     * @see AppserverIo\RemoteMethodInvocation\SessionInterface::send()
179
     * @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...
180
     */
181
    public function send(RemoteMethodInterface $remoteMethod)
182
    {
183
184
        // create an array to store connection response temporarily
185
        $responses = array();
186
187
        // iterate over all connections and invoke the remote method call
188
        foreach ($this->getConnections() as $key => $connection) {
189
            // invoke the remote method on the connection
190
            $responses[$key] = $connection->send($remoteMethod);
191
192
            // check if a proxy has been returned
193
            if (method_exists($responses[$key], 'setSession')) {
194
                $responses[$key]->setSession($this);
195
            }
196
        }
197
198
        // return the response of the first connection
199
        return reset($responses);
200
    }
201
202
    /**
203
     * Creates a remote initial context instance.
204
     *
205
     * @return \AppserverIo\RemoteMethodInvocation\RemoteObjectInterface The proxy for the initial context
206
     * @see \AppserverIo\RemoteMethodInvocation\SessionInterface::createInitialContext()
207
     */
208
    public function createInitialContext()
209
    {
210
        $initialContext = new InitialContextProxy();
211
        $initialContext->__setSession($this);
212
        return $initialContext;
213
    }
214
}
215