SocketCluster::receive()   A
last analyzed

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
3
namespace LaravelSocketCluster;
4
5
use WebSocket\Client;
6
use Closure;
7
use Exception;
8
9
class SocketCluster
10
{
11
    /**
12
     * @var WebSocket\Client
13
     */
14
    protected $websocket;
15
16
    /**
17
     * @var string
18
     */
19
    protected $error;
20
21
    /**
22
     * Construct
23
     *
24
     * @param Client $websocket Websocket
25
     */
26
    public function __construct(Client $websocket)
27
    {
28
        $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<LaravelSocketCluster\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...
29
    }
30
31
    /**
32
     * Publish Channel
33
     *
34
     * @param string       $channel
35
     * @param mixed        $data
36
     * @param Closure|null $callback
37
     *
38
     * @return void
39
     */
40
    public function publish($channel, $data, Closure $callback = null)
41
    {
42
        $pubData = [
43
            'channel' => $channel,
44
            'data' => $data,
45
        ];
46
        
47
        return $this->emit('#publish', $pubData, $callback);
48
    }
49
50
    /**
51
     * Emit Event
52
     *
53
     * @param string       $event
54
     * @param array        $data
55
     * @param Closure|null $callback
56
     *
57
     * @return void
58
     */
59
    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...
60
    {
61
        try {
62
            $eventData = [
63
                'event' => $event,
64
                'data' => $data,
65
            ];
66
67
            $sendData = (string) @json_encode($eventData);
68
69
            $this->websocket->send($sendData);
70
71
            $this->error = null;
72
73
            return true;
74
75
        } catch (Exception $e) {
76
            $this->error = $e->getMessage();
77
            return false;
78
        }
79
    }
80
81
    /**
82
     * Receive
83
     *
84
     * @return string
85
     */
86
    public function receive()
87
    {
88
        return $this->websocket->receive();
89
    }
90
91
    /**
92
     * Get Error
93
     *
94
     * @return string|null
95
     */
96
    public function error()
97
    {
98
        return $this->error;
99
    }
100
}
101