Completed
Pull Request — master (#36)
by Timo
02:49
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 Tidal\WampWatch\ClientSessionInterface;
22
use Tidal\WampWatch\Exception\UnknownProcedureException;
23
use Tidal\WampWatch\Exception\UnknownTopicException;
24
25
/**
26
 * !!! WARNING !!!!
27
 * This Class should only be used for testing or demos.
28
 * It allows for testing client method calls but behaves differently to
29
 * real client session implementation in that it only stores one (the last)
30
 * subscription, registration etc. for a specific topic/procedure.
31
 */
32
33
/**
34
 * Class ClientSessionStub.
35
 */
36
class ClientSessionStub implements ClientSessionInterface, EventEmitterInterface
37
{
38
    use EventEmitterTrait;
39
40
    protected $sessionId;
41
42
    protected $subscriptions = [];
43
44
    protected $publications = [];
45
46
    protected $registrations = [];
47
48
    protected $unregistrations = [];
49
50
    protected $calls = [];
51
52
    protected $procedures = [];
53
54
    /**
55
     * Subscribe.
56
     *
57
     * @param string   $topicName
58
     * @param callable $callback
59
     * @param          $options   array
60
     *
61
     * @return \React\Promise\Promise
62
     */
63
    public function subscribe($topicName, callable $callback, $options = null)
64
    {
65
        $this->on($topicName, $callback);
66
67
        $futureResult = new Deferred();
68
69
        $this->subscriptions[$topicName] = $futureResult;
70
71
        return $futureResult->promise();
72
    }
73
74
    /**
75
     * Trigger a SUBSCRIBED message for given topic.
76
     *
77
     * @param $topicName
78
     * @param $requestId
79
     * @param $subscriptionId
80
     *
81
     * @throws UnknownTopicException if the topic is unknown
82
     */
83 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...
84
    {
85
        if (!isset($this->subscriptions[$topicName])) {
86
            throw new UnknownTopicException($topicName);
87
        }
88
89
        /* @var $futureResult Deferred */
90
        $futureResult = $this->subscriptions[$topicName];
91
        $result = new SubscribedMessage($requestId, $subscriptionId);
92
93
        $futureResult->resolve($result);
94
    }
95
96
    public function hasSubscription($topicName)
97
    {
98
        return isset($this->subscriptions[$topicName]);
99
    }
100
101
    /**
102
     * Publish.
103
     *
104
     * @param string      $topicName
105
     * @param array|mixed $arguments
106
     * @param array|mixed $argumentsKw
107
     * @param array|mixed $options
108
     *
109
     * @return \React\Promise\Promise
110
     */
111
    public function publish($topicName, $arguments = null, $argumentsKw = null, $options = null)
112
    {
113
        $futureResult = new Deferred();
114
115
        $this->publications[$topicName] = $futureResult;
116
117
        return $futureResult->promise();
118
    }
119
120
    /**
121
     * Trigger a PUBLISHED message for given topic.
122
     *
123
     * @param string $topicName
124
     * @param int    $requestId
125
     * @param int    $publicationId
126
     *
127
     * @throws UnknownTopicException if the topic is unknown
128
     */
129 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...
130
    {
131
        if (!isset($this->publications[$topicName])) {
132
            throw new UnknownTopicException($topicName);
133
        }
134
135
        $futureResult = $this->publications[$topicName];
136
        $result = new PublishedMessage($requestId, $publicationId);
137
138
        $futureResult->resolve($result);
139
    }
140
141 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...
142
    {
143
        if (!isset($this->publications[$topicName])) {
144
            throw new UnknownTopicException($topicName);
145
        }
146
147
        $futureResult = $this->publications[$topicName];
148
        $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...
149
150
        $futureResult->reject($error);
151
    }
152
153
    /**
154
     * Register.
155
     *
156
     * @param string      $procedureName
157
     * @param callable    $callback
158
     * @param array|mixed $options
159
     *
160
     * @return \React\Promise\Promise
161
     */
162
    public function register($procedureName, callable $callback, $options = null)
163
    {
164
        $this->procedures[$procedureName] = $callback;
165
166
        $futureResult = new Deferred();
167
168
        $this->registrations[$procedureName] = $futureResult;
169
170
        return $futureResult->promise();
171
    }
172
173
    /**
174
     * Trigger a REGISTERED message for given procedure.
175
     *
176
     * @param string $procedureName
177
     * @param int    $requestId
178
     * @param int    $registrationId
179
     *
180
     * @throws UnknownProcedureException if the procedure is unknown
181
     */
182 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...
183
    {
184
        if (!isset($this->registrations[$procedureName])) {
185
            throw new UnknownProcedureException($procedureName);
186
        }
187
188
        $futureResult = $this->registrations[$procedureName];
189
        $result = new RegisteredMessage($requestId, $registrationId);
190
191
        $futureResult->resolve($result);
192
    }
193
194
    /**
195
     * Triggers a call to a registered procedure and returns its result.
196
     *
197
     * @param string $procedureName
198
     * @param array  $args
199
     *
200
     * @throws UnknownProcedureException if the procedure is unknown
201
     *
202
     * @return mixed the procedure result
203
     */
204
    public function callRegistration($procedureName, array $args = [])
205
    {
206
        if (!isset($this->procedures[$procedureName])) {
207
            throw new UnknownProcedureException($procedureName);
208
        }
209
210
        $procedure = $this->procedures[$procedureName];
211
212
        return $procedure($args);
213
    }
214
215
    /**
216
     * Unregister.
217
     *
218
     * @param string $procedureName
219
     *
220
     * @return \React\Promise\PromiseInterface
221
     */
222
    public function unregister($procedureName)
223
    {
224
        $futureResult = new Deferred();
225
226
        $this->unregistrations[$procedureName] = $futureResult;
227
228
        return $futureResult->promise();
229
    }
230
231
    /**
232
     * Triggers a UNREGISTERED message for given procedure.
233
     *
234
     * @param string $procedureName
235
     * @param int    $requestId
236
     *
237
     * @throws UnknownProcedureException
238
     */
239 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...
240
    {
241
        if (!isset($this->unregistrations[$procedureName])) {
242
            throw new UnknownProcedureException($procedureName);
243
        }
244
245
        $futureResult = $this->unregistrations[$procedureName];
246
        $result = new UnregisteredMessage($requestId);
247
248
        $futureResult->resolve($result);
249
    }
250
251
    /**
252
     * Call.
253
     *
254
     * @param string      $procedureName
255
     * @param array|mixed $arguments
256
     * @param array|mixed $argumentsKw
257
     * @param array|mixed $options
258
     *
259
     * @return \React\Promise\Promise
260
     */
261
    public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null)
262
    {
263
        $futureResult = new Deferred();
264
265
        $this->calls[$procedureName] = $futureResult;
266
267
        return $futureResult->promise();
268
    }
269
270
    /**
271
     * Process ResultMessage.
272
     *
273
     * @param string    $procedureName
274
     * @param \stdClass $result
275
     */
276
    public function respondToCall($procedureName, $result)
277
    {
278
        if (!isset($this->calls[$procedureName])) {
279
            throw new UnknownProcedureException($procedureName);
280
        }
281
282
        /* @var $futureResult Deferred */
283
        $futureResult = $this->calls[$procedureName];
284
285
        $futureResult->resolve($result);
286
    }
287
288
    public function failCall($procedureName, $error)
289
    {
290
        if (!isset($this->calls[$procedureName])) {
291
            throw new UnknownProcedureException($procedureName);
292
        }
293
294
        /* @var $futureResult Deferred */
295
        $futureResult = $this->calls[$procedureName];
296
297
        $futureResult->reject($error);
298
    }
299
300
    public function hasCall($procedureName)
301
    {
302
        return isset($this->calls[$procedureName]);
303
    }
304
305
    /**
306
     * @param int $sessionId
307
     */
308
    public function setSessionId($sessionId)
309
    {
310
        $this->sessionId = $sessionId;
311
    }
312
313
    /**
314
     * @return int the Session Id
315
     */
316
    public function getSessionId()
317
    {
318
        return $this->sessionId;
319
    }
320
321
    /**
322
     * Generate a unique id for sessions and requests.
323
     *
324
     * @return mixed
325
     */
326
    public static function getUniqueId()
327
    {
328
        $filter = 0x1fffffffffffff; // 53 bits
329
        $randomBytes = openssl_random_pseudo_bytes(8);
330
        list($high, $low) = array_values(unpack('N2', $randomBytes));
331
332
        return abs(($high << 32 | $low) & $filter);
333
    }
334
335
    public function sendMessage($msg)
336
    {
337
    }
338
}
339