Completed
Push — master ( dac9d4...d53965 )
by Timo
38s
created

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