Completed
Pull Request — master (#36)
by Timo
06:44
created

ClientSessionStub::failCall()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Tidal/WampWatch package.
5
 *   (c) 2016 Timo Michna <timomichna/yahoo.de>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 *
10
 */
11
12
namespace Tidal\WampWatch\Stub;
13
14
use Evenement\EventEmitterInterface;
15
use Evenement\EventEmitterTrait;
16
use React\Promise\Deferred;
17
use Thruway\Message\PublishedMessage;
18
use Thruway\Message\RegisteredMessage;
19
use Thruway\Message\SubscribedMessage;
20
use Thruway\Message\UnregisteredMessage;
21
use Thruway\Message\ErrorMessage;
22
use Tidal\WampWatch\ClientSessionInterface;
23
use Tidal\WampWatch\Exception\UnknownProcedureException;
24
use Tidal\WampWatch\Exception\UnknownTopicException;
25
26
/**
27
 * !!! WARNING !!!!
28
 * This Class should only be used for testing or demos.
29
 * It allows for testing client method calls but behaves differently to
30
 * real client session implementation in that it only stores one (the last)
31
 * subscription, registration etc. for a specific topic/procedure.
32
 */
33
34
/**
35
 * Class ClientSessionStub.
36
 */
37
class ClientSessionStub implements ClientSessionInterface, EventEmitterInterface
38
{
39
    use EventEmitterTrait;
40
41
    protected $sessionId;
42
43
    protected $subscriptions = [];
44
45
    protected $publications = [];
46
47
    protected $registrations = [];
48
49
    protected $unregistrations = [];
50
51
    protected $calls = [];
52
53
    protected $procedures = [];
54
55
    /**
56
     * Subscribe.
57
     *
58
     * @param string   $topicName
59
     * @param callable $callback
60
     * @param          $options   array
61
     *
62
     * @return \React\Promise\Promise
63
     */
64
    public function subscribe($topicName, callable $callback, $options = null)
65
    {
66
        $this->on($topicName, $callback);
67
68
        $futureResult = new Deferred();
69
70
        $this->subscriptions[$topicName] = $futureResult;
71
72
        return $futureResult->promise();
73
    }
74
75
    /**
76
     * Trigger a SUBSCRIBED message for given topic.
77
     *
78
     * @param $topicName
79
     * @param $requestId
80
     * @param $subscriptionId
81
     *
82
     * @throws UnknownTopicException if the topic is unknown
83
     */
84 View Code Duplication
    public function completeSubscription($topicName, $requestId = 1, $subscriptionId = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (!isset($this->subscriptions[$topicName])) {
87
            throw new UnknownTopicException($topicName);
88
        }
89
90
        /* @var $futureResult Deferred */
91
        $futureResult = $this->subscriptions[$topicName];
92
        $result = new SubscribedMessage($requestId, $subscriptionId);
93
94
        $futureResult->resolve($result);
95
    }
96
97
    public function hasSubscription($topicName)
98
    {
99
        return isset($this->subscriptions[$topicName]);
100
    }
101
102
    /**
103
     * Publish.
104
     *
105
     * @param string      $topicName
106
     * @param array|mixed $arguments
107
     * @param array|mixed $argumentsKw
108
     * @param array|mixed $options
109
     *
110
     * @return \React\Promise\Promise
111
     */
112
    public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null)
113
    {
114
        $futureResult = new Deferred();
115
116
        $this->publications[$topicName] = $futureResult;
117
118
        return $futureResult->promise();
119
    }
120
121
    /**
122
     * Trigger a PUBLISHED message for given topic.
123
     *
124
     * @param string $topicName
125
     * @param int    $requestId
126
     * @param int    $publicationId
127
     *
128
     * @throws UnknownTopicException if the topic is unknown
129
     */
130 View Code Duplication
    public function confirmPublication($topicName, $requestId = 1, $publicationId = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132
        if (!isset($this->publications[$topicName])) {
133
            throw new UnknownTopicException($topicName);
134
        }
135
136
        $futureResult = $this->publications[$topicName];
137
        $result = new PublishedMessage($requestId, $publicationId);
138
139
        $futureResult->resolve($result);
140
    }
141
142 View Code Duplication
    public function failPublication($topicName, $error)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        if (!isset($this->publications[$topicName])) {
145
            throw new UnknownTopicException($topicName);
146
        }
147
148
        $futureResult = $this->publications[$topicName];
149
        $result = new PublishedMessage($requestId, $publicationId);
0 ignored issues
show
Bug introduced by
The variable $requestId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $publicationId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
151
        $futureResult->reject($error);
152
    }
153
154
    /**
155
     * Register.
156
     *
157
     * @param string      $procedureName
158
     * @param callable    $callback
159
     * @param array|mixed $options
160
     *
161
     * @return \React\Promise\Promise
162
     */
163
    public function register($procedureName, callable $callback, $options = null)
164
    {
165
        $this->procedures[$procedureName] = $callback;
166
167
        $futureResult = new Deferred();
168
169
        $this->registrations[$procedureName] = $futureResult;
170
171
        return $futureResult->promise();
172
    }
173
174
    /**
175
     * Trigger a REGISTERED message for given procedure.
176
     *
177
     * @param string $procedureName
178
     * @param int    $requestId
179
     * @param int    $registrationId
180
     *
181
     * @throws UnknownProcedureException if the procedure is unknown
182
     */
183 View Code Duplication
    public function confirmRegistration($procedureName, $requestId = 1, $registrationId = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
    {
185
        if (!isset($this->registrations[$procedureName])) {
186
            throw new UnknownProcedureException($procedureName);
187
        }
188
189
        $futureResult = $this->registrations[$procedureName];
190
        $result = new RegisteredMessage($requestId, $registrationId);
191
192
        $futureResult->resolve($result);
193
    }
194
195
    /**
196
     * Triggers a call to a registered procedure and returns its result.
197
     *
198
     * @param string $procedureName
199
     * @param array  $args
200
     *
201
     * @throws UnknownProcedureException if the procedure is unknown
202
     *
203
     * @return mixed the procedure result
204
     */
205
    public function callRegistration($procedureName, array $args = [])
206
    {
207
        if (!isset($this->procedures[$procedureName])) {
208
            throw new UnknownProcedureException($procedureName);
209
        }
210
211
        $procedure = $this->procedures[$procedureName];
212
213
        return $procedure($args);
214
    }
215
216
    /**
217
     * Unregister.
218
     *
219
     * @param string $procedureName
220
     *
221
     * @return \React\Promise\Promise|false
222
     */
223
    public function unregister($procedureName)
224
    {
225
        $futureResult = new Deferred();
226
227
        $this->unregistrations[$procedureName] = $futureResult;
228
229
        return $futureResult->promise();
230
    }
231
232
    /**
233
     * Triggers a UNREGISTERED message for given procedure.
234
     *
235
     * @param string $procedureName
236
     * @param int    $requestId
237
     *
238
     * @throws UnknownProcedureException
239
     */
240 View Code Duplication
    public function confirmUnregistration($procedureName, $requestId = 1)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
241
    {
242
        if (!isset($this->unregistrations[$procedureName])) {
243
            throw new UnknownProcedureException($procedureName);
244
        }
245
246
        $futureResult = $this->unregistrations[$procedureName];
247
        $result = new UnregisteredMessage($requestId);
248
249
        $futureResult->resolve($result);
250
    }
251
252
    /**
253
     * Call.
254
     *
255
     * @param string      $procedureName
256
     * @param array|mixed $arguments
257
     * @param array|mixed $argumentsKw
258
     * @param array|mixed $options
259
     *
260
     * @return \React\Promise\Promise
261
     */
262
    public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null)
263
    {
264
        $futureResult = new Deferred();
265
266
        $this->calls[$procedureName] = $futureResult;
267
268
        return $futureResult->promise();
269
    }
270
271
    /**
272
     * Process ResultMessage.
273
     *
274
     * @param string    $procedureName
275
     * @param \stdClass $result
276
     */
277
    public function respondToCall($procedureName, $result)
278
    {
279
        if (!isset($this->calls[$procedureName])) {
280
            throw new UnknownProcedureException($procedureName);
281
        }
282
283
        /* @var $futureResult Deferred */
284
        $futureResult = $this->calls[$procedureName];
285
286
        $futureResult->resolve($result);
287
    }
288
289
    public function failCall($procedureName, $error)
290
    {
291
        if (!isset($this->calls[$procedureName])) {
292
            throw new UnknownProcedureException($procedureName);
293
        }
294
295
        /* @var $futureResult Deferred */
296
        $futureResult = $this->calls[$procedureName];
297
298
        $futureResult->reject($error);
299
    }
300
301
302
303
    public function hasCall($procedureName)
304
    {
305
        return isset($this->calls[$procedureName]);
306
    }
307
308
    /**
309
     * @param int $sessionId
310
     */
311
    public function setSessionId($sessionId)
312
    {
313
        $this->sessionId = $sessionId;
314
    }
315
316
    /**
317
     * @return int the Session Id
318
     */
319
    public function getSessionId()
320
    {
321
        return $this->sessionId;
322
    }
323
324
    /**
325
     * Generate a unique id for sessions and requests.
326
     *
327
     * @return mixed
328
     */
329
    public static function getUniqueId()
330
    {
331
        $filter = 0x1fffffffffffff; // 53 bits
332
        $randomBytes = openssl_random_pseudo_bytes(8);
333
        list($high, $low) = array_values(unpack('N2', $randomBytes));
334
335
        return abs(($high << 32 | $low) & $filter);
336
    }
337
338
    public function sendMessage($msg)
339
    {
340
    }
341
}
342