SocketCluster::emit()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
cc 2
eloc 12
nc 3
nop 2
crap 2
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 4
    public function __construct(Client $websocket)
26
    {
27 4
        $this->websocket = $websocket;
28 4
    }
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);
0 ignored issues
show
Unused Code introduced by
The call to SocketCluster::emit() has too many arguments starting with $callback.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
    }
48
49
    /**
50
     * Emit Event
51
     *
52
     * @param string $event
53
     * @param array  $data
54
     *
55
     * @return boolean
56
     */
57 2
    protected function emit($event, array $data)
58
    {
59
        try {
60
            $eventData = [
61 2
                'event' => $event,
62 2
                'data' => $data,
63 2
            ];
64
65 2
            $sendData = (string) @json_encode($eventData);
66
67 2
            $this->websocket->send($sendData);
68
69 1
            $this->error = null;
70
71 1
            return true;
72
73 1
        } catch (Exception $e) {
74 1
            $this->error = $e->getMessage();
75 1
            return false;
76
        }
77
    }
78
79
    /**
80
     * Get Error
81
     *
82
     * @return string
83
     */
84 1
    public function error()
85
    {
86 1
        return $this->error;
87
    }
88
}
89