Completed
Push — master ( f6c83d...982358 )
by Vasily
06:50
created

ExamplePubSubWebSocketRoute::onFinish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
namespace PHPDaemon\Examples;
3
4
class ExamplePubSubWebSocketRoute extends ExampleWebSocketRoute
5
{
6
    use \PHPDaemon\Traits\StaticObjectWatchdog;
7
8
    /**
9
     * Called when new frame received.
10
     * @param string  Frame's contents.
11
     * @param integer Frame's type.
12
     * @return void
13
     */
14
    public function onFrame($data, $type)
15
    {
16
        $ws = $this;
17
        $req = json_decode($data, true);
18
        if ($req['type'] === 'subscribe') {
19
            $eventName = $req['event'];
20
            $this->appInstance->pubsub->sub($req['event'], $this, function ($data) use ($ws, $eventName) {
0 ignored issues
show
Bug introduced by
The property pubsub does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
21
                $ws->sendObject([
22
                    'type' => 'event',
23
                    'event' => $eventName,
24
                    'data' => $data,
25
                ]);
26
            });
27
        }
28
    }
29
30
    public function sendObject($obj)
31
    {
32
        $this->client->sendFrame(json_encode($obj), 'STRING');
33
    }
34
35
    public function onFinish()
36
    {
37
        $this->appInstance->pubsub->unsubFromAll($this);
0 ignored issues
show
Bug introduced by
The property pubsub does not seem to exist in PHPDaemon\Core\AppInstance.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
38
    }
39
}
40