SocketCluster   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 5
c 5
b 2
f 0
lcom 1
cbo 1
dl 0
loc 81
ccs 21
cts 21
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A publish() 0 9 1
A emit() 0 21 2
A error() 0 4 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 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