Session::poll()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
nc 2
nop 1
1
<?php
2
namespace PHPDaemon\SockJS;
3
4
use PHPDaemon\Core\Daemon;
5
use PHPDaemon\Core\Timer;
6
use PHPDaemon\HTTPRequest\Generic;
7
use PHPDaemon\Structures\StackCallbacks;
8
9
/**
10
 * @package    Libraries
11
 * @subpackage SockJS
12
 * @author     Vasily Zorin <[email protected]>
13
 */
14
class Session
15
{
16
    use \PHPDaemon\Traits\ClassWatchdog;
17
    use \PHPDaemon\Traits\StaticObjectWatchdog;
18
19
    /**
20
     * @var \PHPDaemon\Request\Generic
21
     */
22
    public $route;
23
24
    /**
25
     * @var \PHPDaemon\Structures\StackCallbacks
26
     */
27
    public $onWrite;
28
29
    public $id;
30
31
    public $appInstance;
32
33
    public $addr;
34
35
36
    /**
37
     * @var array
38
     */
39
    public $buffer = [];
40
41
    public $framesBuffer = [];
42
43
    /**
44
     * @var boolean
45
     */
46
    public $finished = false;
47
48
    protected $onFinishedCalled = false;
49
50
    /**
51
     * @var boolean
52
     */
53
    public $flushing = false;
54
55
    /**
56
     * @var integer
57
     */
58
    public $timeout = 60;
59
    public $server;
60
    public $get;
61
    public $cookie;
62
    public $post;
63
64
    protected $pollMode;
65
66
    protected $running = false;
67
68
    protected $finishTimer;
69
70
    /**
71
     * toJson
72
     * @param  string $m
73
     * @return string
74
     */
75
    protected function toJson($m)
76
    {
77
        return json_encode($m, JSON_UNESCAPED_SLASHES);
78
    }
79
80
    /**
81
     * __construct
82
     * @param Application $appInstance [@todo description]
83
     * @param string $id [@todo description]
84
     * @param array $server [@todo description]
85
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
86
     */
87
    public function __construct($appInstance, $id, $server)
88
    {
89
        $this->onWrite = new StackCallbacks;
90
        $this->id = $id;
91
        $this->appInstance = $appInstance;
92
        $this->server = $server;
93
94 View Code Duplication
        if (isset($this->server['HTTP_COOKIE'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
            Generic::parseStr(strtr($this->server['HTTP_COOKIE'], Generic::$hvaltr), $this->cookie);
96
        }
97
        if (isset($this->server['QUERY_STRING'])) {
98
            Generic::parseStr($this->server['QUERY_STRING'], $this->get);
99
        }
100
101
        $this->addr = $server['REMOTE_ADDR'];
102
        $this->finishTimer = setTimeout(function ($timer) {
0 ignored issues
show
Unused Code introduced by
The parameter $timer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
            $this->finish();
104
        }, $this->timeout * 1e6);
105
106
        $this->appInstance->subscribe('c2s:' . $this->id, [$this, 'c2s']);
107
        $this->appInstance->subscribe('poll:' . $this->id, [$this, 'poll'], function ($redis) {
0 ignored issues
show
Unused Code introduced by
The parameter $redis is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
            $this->appInstance->publish('state:' . $this->id, 'started', function ($redis) {
0 ignored issues
show
Unused Code introduced by
The parameter $redis is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
                // @TODO: remove this callback
110
            });
111
        });
112
    }
113
114
    /**
115
     * Uncaught exception handler
116
     * @param  object $e
117
     * @return boolean|null Handled?
118
     */
119
    public function handleException($e)
120
    {
121
        if (!isset($this->route)) {
122
            return false;
123
        }
124
        return $this->route->handleException($e);
125
    }
126
127
    /**
128
     * onHandshake
129
     * @return void
130
     */
131
    public function onHandshake()
132
    {
133
        if (!isset($this->route)) {
134
            return;
135
        }
136
        $this->route->onWakeup();
137
        try {
138
            $this->route->onHandshake();
0 ignored issues
show
Documentation Bug introduced by
The method onHandshake does not exist on object<PHPDaemon\Request\Generic>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
139
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
140
            Daemon::uncaughtExceptionHandler($e);
141
        }
142
        $this->route->onSleep();
143
    }
144
145
    /**
146
     * c2s
147
     * @param  object $redis
148
     * @return void
149
     */
150
    public function c2s($redis)
151
    {
152
        if (!$redis) {
153
            return;
154
        }
155
        if ($this->finished) {
156
            return;
157
        }
158
        list(, $chan, $msg) = $redis->result;
0 ignored issues
show
Unused Code introduced by
The assignment to $chan is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
159
        if ($msg === '') {
160
            return;
161
        }
162
        $this->onFrame($msg, \PHPDaemon\Servers\WebSocket\Pool::STRING);
163
    }
164
165
    /**
166
     * onFrame
167
     * @param  string $msg [@todo description]
168
     * @param  integer $type [@todo description]
169
     * @return void
170
     */
171
    public function onFrame($msg, $type)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        $frames = json_decode($msg, true);
174
        if (!is_array($frames)) {
175
            return;
176
        }
177
        $this->route->onWakeup();
178
        foreach ($frames as $frame) {
179
            try {
180
                $this->route->onFrame($frame, \PHPDaemon\Servers\WebSocket\Pool::STRING);
0 ignored issues
show
Documentation Bug introduced by
The method onFrame does not exist on object<PHPDaemon\Request\Generic>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
181
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
182
                Daemon::uncaughtExceptionHandler($e);
183
            }
184
        }
185
        $this->route->onSleep();
186
    }
187
188
    /**
189
     * poll
190
     * @param  object $redis
191
     * @return void
192
     */
193
    public function poll($redis)
194
    {
195
        if (!$redis) {
196
            return;
197
        }
198
        list(, $chan, $msg) = $redis->result;
0 ignored issues
show
Unused Code introduced by
The assignment to $chan is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
199
        $this->pollMode = json_decode($msg, true);
200
201
        Timer::setTimeout($this->finishTimer);
202
        $this->flush();
203
    }
204
205
    /**
206
     * @TODO DESCR
207
     * @return void
208
     */
209
    public function onWrite()
210
    {
211
        $this->onWrite->executeAll($this->route);
212
        if (method_exists($this->route, 'onWrite')) {
213
            $this->route->onWrite();
214
        }
215
        if ($this->finished) {
216
            if (!sizeof($this->buffer) && !sizeof($this->framesBuffer)) {
217
                $this->onFinish();
218
            }
219
        }
220
        Timer::setTimeout($this->finishTimer);
221
    }
222
223
    /**
224
     * @TODO DESCR
225
     * @return void
226
     */
227
    public function finish()
228
    {
229
        if ($this->finished) {
230
            return;
231
        }
232
        $this->finished = true;
233
        $this->onFinish();
234
        $this->sendPacket('c' . json_encode([3000, 'Go away!']));
0 ignored issues
show
Documentation introduced by
'c' . json_encode(array(3000, 'Go away!')) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
    }
236
237
    /*public function __destruct() {
238
        D('destructed session '.$this->id);
239
    }*/
240
241
    /**
242
     * @TODO DESCR
243
     * @return void
244
     */
245
    public function onFinish()
246
    {
247
        if ($this->onFinishedCalled) {
248
            return;
249
        }
250
        $this->onFinishedCalled = true;
251
        $this->appInstance->unsubscribe('c2s:' . $this->id, [$this, 'c2s']);
252
        $this->appInstance->unsubscribe('poll:' . $this->id, [$this, 'poll']);
253
        if (isset($this->route)) {
254
            $this->route->onFinish();
255
        }
256
        $this->onWrite->reset();
257
        $this->route = null;
258
        Timer::remove($this->finishTimer);
259
        $this->appInstance->endSession($this);
260
    }
261
262
    /**
263
     * Flushes buffered packets
264
     * @return void
265
     */
266
    public function flush()
267
    {
268
        if ($this->pollMode === null) { // first polling request is not there yet
269
            return;
270
        }
271
        if ($this->flushing) {
272
            return;
273
        }
274
        $bsize = sizeof($this->buffer);
275
        $fbsize = sizeof($this->framesBuffer);
276
        if ($bsize === 0 && $fbsize === 0) {
277
            return;
278
        }
279
        $this->flushing = true;
280
        if (in_array('one-by-one', $this->pollMode)) {
281
            $b = array_slice($this->buffer, 0, 1);
282
            $bsize = sizeof($b);
283
        } else {
284
            $b = $this->buffer;
285
        }
286
        if ($fbsize > 0) {
287
            if (!in_array('one-by-one', $this->pollMode) || !sizeof($b)) {
288
                $b[] = 'a' . $this->toJson($this->framesBuffer);
0 ignored issues
show
Documentation introduced by
$this->framesBuffer is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
289
            } else {
290
                $fbsize = 0;
291
            }
292
        }
293
        $this->appInstance->publish(
294
            's2c:' . $this->id,
295
            $this->toJson($b),
0 ignored issues
show
Documentation introduced by
$b is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
296
            function ($redis) use ($bsize, $fbsize, $b) {
297
                $this->flushing = false;
298
                if (!$redis) {
299
                    return;
300
                }
301
                //D(['b' => $b, $redis->result]);
302
                if ($redis->result === 0) {
303
                    return;
304
                }
305
                $reflush = false;
306 View Code Duplication
                if (sizeof($this->buffer) > $bsize) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
307
                    $this->buffer = array_slice($this->buffer, $bsize);
308
                    $reflush = true;
309
                } else {
310
                    $this->buffer = [];
311
                }
312
313 View Code Duplication
                if (sizeof($this->framesBuffer) > $fbsize) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
314
                    $this->framesBuffer = array_slice($this->framesBuffer, $fbsize);
315
                    $reflush = true;
316
                } else {
317
                    $this->framesBuffer = [];
318
                }
319
                $this->onWrite();
320
                if ($reflush && in_array('stream', $this->pollMode)) {
321
                    $this->flush();
322
                }
323
            }
324
        );
325
    }
326
327
    /**
328
     * sendPacket
329
     * @param  object $pct [@todo description]
330
     * @param  callable $cb [@todo description]
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
331
     * @callback $cb ( )
332
     * @return void
333
     */
334
    public function sendPacket($pct, $cb = null)
335
    {
336
        if (sizeof($this->framesBuffer)) {
337
            $this->buffer[] = 'a' . $this->toJson($this->framesBuffer);
0 ignored issues
show
Documentation introduced by
$this->framesBuffer is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
338
            $this->framesBuffer = [];
339
        }
340
        $this->buffer[] = $pct;
341
        if ($cb !== null) {
342
            $this->onWrite->push($cb);
343
        }
344
        $this->flush();
345
    }
346
347
    /**
348
     * Sends a frame.
349
     * @param  string $data Frame's data.
350
     * @param  integer $type Frame's type. See the constants.
351
     * @param  callback $cb Optional. Callback called when the frame is received by client.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
352
     * @callback $cb ( )
353
     * @return boolean Success.
354
     */
355
    public function sendFrame($data, $type = 0x00, $cb = null)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
356
    {
357
        if ($this->finished) {
358
            return false;
359
        }
360
        $this->framesBuffer[] = $data;
361
        if ($cb !== null) {
362
            $this->onWrite->push($cb);
363
        }
364
        $this->flush();
365
        return true;
366
    }
367
}
368