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

ClientSessionStub::callRegistration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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