Completed
Pull Request — master (#16)
by Timo
03:16
created

ClientSessionStub::hasCall()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
    /**
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
    /**
142
     * Register.
143
     *
144
     * @param string      $procedureName
145
     * @param callable    $callback
146
     * @param array|mixed $options
147
     *
148
     * @return \React\Promise\Promise
149
     */
150
    public function register($procedureName, callable $callback, $options = null)
151
    {
152
        $this->procedures[$procedureName] = $callback;
153
154
        $futureResult = new Deferred();
155
156
        $this->registrations[$procedureName] = $futureResult;
157
158
159
        return $futureResult->promise();
160
    }
161
162
    /**
163
     * Trigger a REGISTERED message for given procedure.
164
     *
165
     * @param string $procedureName
166
     * @param int    $requestId
167
     * @param int    $registrationId
168
     *
169
     * @throws UnknownProcedureException if the procedure is unknown.
170
     */
171 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...
172
    {
173
        if (!isset($this->registrations[$procedureName])) {
174
            throw new UnknownProcedureException($procedureName);
175
        }
176
177
        $futureResult = $this->registrations[$procedureName];
178
        $result = new RegisteredMessage($requestId, $registrationId);
179
180
        $futureResult->resolve($result);
181
    }
182
183
    /**
184
     * Triggers a call to a registered procedure and returns its result.
185
     *
186
     * @param string $procedureName
187
     * @param array  $args
188
     *
189
     * @throws UnknownProcedureException if the procedure is unknown.
190
     *
191
     * @return mixed the procedure result
192
     */
193
    public function callRegistration($procedureName, array $args = [])
194
    {
195
        if (!isset($this->procedures[$procedureName])) {
196
            throw new UnknownProcedureException($procedureName);
197
        }
198
199
        $procedure = $this->procedures[$procedureName];
200
201
        return $procedure($args);
202
    }
203
204
    /**
205
     * Unregister.
206
     *
207
     * @param string $procedureName
208
     *
209
     * @return \React\Promise\Promise|false
210
     */
211
    public function unregister($procedureName)
212
    {
213
        $futureResult = new Deferred();
214
215
        $this->unregistrations[$procedureName] = $futureResult;
216
217
218
        return $futureResult->promise();
219
    }
220
221
    /**
222
     * Triggers a UNREGISTERED message for given procedure.
223
     *
224
     * @param string $procedureName
225
     * @param int    $requestId
226
     *
227
     * @throws UnknownProcedureException
228
     */
229 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...
230
    {
231
        if (!isset($this->unregistrations[$procedureName])) {
232
            throw new UnknownProcedureException($procedureName);
233
        }
234
235
        $futureResult = $this->unregistrations[$procedureName];
236
        $result = new UnregisteredMessage($requestId);
237
238
        $futureResult->resolve($result);
239
    }
240
241
    /**
242
     * Call.
243
     *
244
     * @param string      $procedureName
245
     * @param array|mixed $arguments
246
     * @param array|mixed $argumentsKw
247
     * @param array|mixed $options
248
     *
249
     * @return \React\Promise\Promise
250
     */
251
    public function call($procedureName, $arguments = null, $argumentsKw = null, $options = null)
252
    {
253
        $futureResult = new Deferred();
254
255
        $this->calls[$procedureName] = $futureResult;
256
257
        return $futureResult->promise();
258
    }
259
260
    /**
261
     * Process ResultMessage.
262
     *
263
     * @param string    $procedureName
264
     * @param \stdClass $result
265
     */
266
    public function respondToCall($procedureName, $result)
267
    {
268
        if (!isset($this->calls[$procedureName])) {
269
            throw new UnknownProcedureException($procedureName);
270
        }
271
272
        /* @var $futureResult Deferred */
273
        $futureResult = $this->calls[$procedureName];
274
275
        $futureResult->resolve($result);
276
    }
277
278
    public function hasCall($procedureName)
279
    {
280
        return isset($this->calls[$procedureName]);
281
    }
282
283
284
    /**
285
     * @param int $sessionId
286
     */
287
    public function setSessionId($sessionId)
288
    {
289
        $this->sessionId = $sessionId;
290
    }
291
292
    /**
293
     * @return int the Session Id
294
     */
295
    public function getSessionId()
296
    {
297
        return $this->sessionId;
298
    }
299
300
    /**
301
     * Generate a unique id for sessions and requests.
302
     *
303
     * @return mixed
304
     */
305
    public static function getUniqueId()
306
    {
307
        $filter = 0x1fffffffffffff; // 53 bits
308
        $randomBytes = openssl_random_pseudo_bytes(8);
309
        list($high, $low) = array_values(unpack('N2', $randomBytes));
310
311
        return abs(($high << 32 | $low) & $filter);
312
    }
313
}
314