Completed
Push — master ( 2e294b...087ce9 )
by Renato
06:42
created

SocketCluster::receive()   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
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace SocketCluster;
3
4
use WebSocket\Client;
5
use Closure;
6
use Exception;
7
8
class SocketCluster
9
{
10
    /**
11
     * @var WebSocket\Client
12
     */
13
    protected $websocket;
14
15
    /**
16
     * @var string
17
     */
18
    protected $error;
19
20
    /**
21
     * Construct
22
     *
23
     * @param WebSocket\Client $websocket
24
     */
25
    public function __construct(Client $websocket)
26
    {
27
        $this->websocket = $websocket;
0 ignored issues
show
Documentation Bug introduced by
It seems like $websocket of type object<WebSocket\Client> is incompatible with the declared type object<SocketCluster\WebSocket\Client> of property $websocket.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
    }
29
30
    /**
31
     * Get Websocket
32
     *
33
     * @return WebSocket\Client
34
     */
35
    public function getWebsocket()
36
    {
37
        return $this->websocket;
38
    }
39
40
    /**
41
     * Publish Channel
42
     *
43
     * @param string       $channel
44
     * @param mixed        $data
45
     * @param Closure|null $callback
46
     *
47
     * @return void
48
     */
49
    public function publish($channel, $data, Closure $callback = null)
50
    {
51
        $pubData = [
52
            'channel' => $channel,
53
            'data' => $data,
54
        ];
55
        
56
        return $this->emit('#publish', $pubData, $callback);
57
    }
58
59
    /**
60
     * Emit Event
61
     *
62
     * @param string       $event
63
     * @param array        $data
64
     * @param Closure|null $callback
65
     *
66
     * @return void
67
     */
68
    public function emit($event, array $data, Closure $callback = null)
0 ignored issues
show
Unused Code introduced by
The parameter $callback 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...
69
    {
70
        try {
71
            $eventData = [
72
                'event' => $event,
73
                'data' => $data,
74
            ];
75
76
            $sendData = (string) @json_encode($eventData);
77
78
            $this->websocket->send($sendData);
79
80
            $this->error = null;
81
82
            return true;
83
84
        } catch (Exception $e) {
85
            $this->error = $e->getMessage();
86
            return false;
87
        }
88
    }
89
90
    /**
91
     * Receive
92
     *
93
     * @return string
94
     */
95
    public function receive()
96
    {
97
        return $this->websocket->receive();
98
    }
99
100
    /**
101
     * Get Error
102
     *
103
     * @return string|null
104
     */
105
    public function error()
106
    {
107
        return $this->error;
108
    }
109
}
110