Completed
Push — master ( 96bc9a...0115c9 )
by Renato
10:37 queued 05:03
created

SocketCluster::publish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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