Completed
Pull Request — master (#36)
by Timo
02:48
created

ClientSessionStub::failPublication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

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 6
nc 2
nop 3
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
    public function failPublication($topicName, $error, $requestId = 1)
143
    {
144
        if (!isset($this->publications[$topicName])) {
145
            throw new UnknownTopicException($topicName);
146
        }
147
148
        $futureResult = $this->publications[$topicName];
149
        $result = new ErrorMessage($error, $requestId, new \stdClass(), $topicName);
150
151
        $futureResult->reject($result);
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\PromiseInterface
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 mixed  $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
    public function hasCall($procedureName)
302
    {
303
        return isset($this->calls[$procedureName]);
304
    }
305
306
    /**
307
     * @param int $sessionId
308
     */
309
    public function setSessionId($sessionId)
310
    {
311
        $this->sessionId = $sessionId;
312
    }
313
314
    /**
315
     * @return int the Session Id
316
     */
317
    public function getSessionId()
318
    {
319
        return $this->sessionId;
320
    }
321
322
    /**
323
     * Generate a unique id for sessions and requests.
324
     *
325
     * @return mixed
326
     */
327
    public static function getUniqueId()
328
    {
329
        $filter = 0x1fffffffffffff; // 53 bits
330
        $randomBytes = openssl_random_pseudo_bytes(8);
331
        list($high, $low) = array_values(unpack('N2', $randomBytes));
332
333
        return abs(($high << 32 | $low) & $filter);
334
    }
335
336
    public function sendMessage($msg)
337
    {
338
    }
339
}
340