WebSocketRouteProxy   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 130
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __get() 0 4 1
A __call() 0 5 1
A onFrame() 0 16 5
A onPacket() 0 4 1
A onBeforeHandshake() 0 7 2
A onHandshake() 0 11 2
A onWrite() 0 6 2
A onFinish() 0 8 2
1
<?php
2
namespace PHPDaemon\SockJS;
3
4
use PHPDaemon\Core\Timer;
5
6
/**
7
 * @package    Libraries
8
 * @subpackage SockJS
9
 * @author     Vasily Zorin <[email protected]>
10
 */
11
class WebSocketRouteProxy implements \PHPDaemon\WebSocket\RouteInterface
12
{
13
    use \PHPDaemon\Traits\StaticObjectWatchdog;
14
15
    protected $heartbeatTimer;
16
17
    protected $realRoute;
18
19
    protected $sockjs;
20
21
    /**
22
     * __construct
23
     * @param Application $sockjs
24
     * @param object $conn
0 ignored issues
show
Bug introduced by
There is no parameter named $conn. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     */
26
    public function __construct($sockjs, $route)
27
    {
28
        $this->sockjs = $sockjs;
29
        $this->realRoute = $route;
30
    }
31
32
    /**
33
     * __get
34
     * @param  string $k
35
     * @return mixed
36
     */
37
    public function &__get($k)
38
    {
39
        return $this->realRoute->{$k};
40
    }
41
42
    /**
43
     * __call
44
     * @param  string $method
45
     * @param  array $args
46
     * @return mixed
47
     */
48
    public function __call($method, $args)
49
    {
50
        $func = [$this->realRoute, $method];
51
        $func(...$args);
52
    }
53
54
    /**
55
     * Called when new frame received.
56
     * @param string $data Frame's contents.
57
     * @param integer $type Frame's type.
58
     * @return void
59
     */
60
    public function onFrame($data, $type)
61
    {
62
        foreach (explode("\n", $data) as $pct) {
63
            if ($pct === '') {
64
                continue;
65
            }
66
            $pct = json_decode($pct, true);
67
            if (isset($pct[0])) {
68
                foreach ($pct as $i) {
69
                    $this->onPacket(rtrim($i, "\n"), $type);
70
                }
71
            } else {
72
                $this->onPacket($pct, $type);
73
            }
74
        }
75
    }
76
77
    /**
78
     * onPacket
79
     * @param string $data Frame's contents.
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
80
     * @param integer $type Frame's type.
81
     * @return void
82
     */
83
    public function onPacket($frame, $type)
84
    {
85
        $this->realRoute->onFrame($frame, $type);
0 ignored issues
show
Bug introduced by
The method onFrame cannot be called on $this->realRoute (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
86
    }
87
88
    /**
89
     * realRoute onBeforeHandshake
90
     * @param  callable $cb
91
     * @return void|false
92
     */
93
    public function onBeforeHandshake($cb)
94
    {
95
        if (!method_exists($this->realRoute, 'onBeforeHandshake')) {
96
            return false;
97
        }
98
        $this->realRoute->onBeforeHandshake($cb);
0 ignored issues
show
Bug introduced by
The method onBeforeHandshake cannot be called on $this->realRoute (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
99
    }
100
101
    /**
102
     * @TODO DESCR
103
     * @return void
104
     */
105
    public function onHandshake()
106
    {
107
        $this->realRoute->client->sendFrameReal('o');
108
        if (($f = $this->sockjs->config->heartbeatinterval->value) > 0) {
109
            $this->heartbeatTimer = setTimeout(function ($timer) {
110
                $this->realRoute->client->sendFrameReal('h');
111
                $timer->timeout();
112
            }, $f * 1e6);
113
            $this->realRoute->onHandshake();
114
        }
115
    }
116
117
    /**
118
     * @TODO DESCR
119
     * @return void
120
     */
121
    public function onWrite()
122
    {
123
        if (method_exists($this->realRoute, 'onWrite')) {
124
            $this->realRoute->onWrite();
0 ignored issues
show
Bug introduced by
The method onWrite cannot be called on $this->realRoute (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
125
        }
126
    }
127
128
    /**
129
     * @TODO DESCR
130
     * @return void
131
     */
132
    public function onFinish()
133
    {
134
        Timer::remove($this->heartbeatTimer);
135
        if ($this->realRoute) {
136
            $this->realRoute->onFinish();
137
            $this->realRoute = null;
138
        }
139
    }
140
}
141