Completed
Push — master ( 99ae35...96bc9a )
by Renato
10:36
created

SocketCluster::getWebsocket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 5
    public function __construct(Client $websocket)
26
    {
27 5
        $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 5
    }
29
30
    /**
31
     * Publish Channel
32
     *
33
     * @param string       $channel
34
     * @param mixed        $data
35 2
     * @param Closure|null $callback
36
     *
37 2
     * @return boolean
38
     */
39
    public function publish($channel, $data, Closure $callback = null)
40
    {
41
        $pubData = [
42
            'channel' => $channel,
43
            'data' => $data,
44
        ];
45
        
46
        return $this->emit('#publish', $pubData, $callback);
47
    }
48
49 1
    /**
50
     * Emit Event
51
     *
52 1
     * @param string       $event
53 1
     * @param array        $data
54 1
     * @param Closure|null $callback
55
     *
56 1
     * @return boolean
57
     */
58
    protected 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...
59
    {
60
        try {
61
            $eventData = [
62
                'event' => $event,
63
                'data' => $data,
64
            ];
65
66
            $sendData = (string) @json_encode($eventData);
67
68 3
            $this->websocket->send($sendData);
69
70
            $this->error = null;
71
72 3
            return true;
73 3
74 3
        } catch (Exception $e) {
75
            $this->error = $e->getMessage();
76 3
            return false;
77
        }
78 3
    }
79
80 2
    /**
81
     * Get Error
82 2
     *
83
     * @return string
84 1
     */
85 1
    public function error()
86 1
    {
87
        return $this->error;
88
    }
89
}
90