Completed
Push — master ( ea20ea...a25393 )
by Timo
41s
created

ClientSessionStub::confirmRegistration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

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