WebSocketAdapter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 39
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A trigger() 0 19 1
A isEnabled() 0 8 2
1
<?php
2
/**
3
 * This file contains functionality to communicate with bzion's PHP websocket
4
 *
5
 * @package    BZiON\NotificationAdapters
6
 * @license    https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3
7
 */
8
9
namespace BZIon\NotificationAdapter;
10
11
use BZIon\Debug\Debug;
12
13
/**
14
 * An interface to our websocket
15
 * @package    BZiON\NotificationAdapters
16
 */
17
class WebSocketAdapter extends NotificationAdapter
18
{
19
    /**
20
     * {@inheritdoc}
21
     *
22
     * @todo Error handling
23
     */
24
    public function trigger($channel, $message)
25
    {
26
        Debug::startStopwatch("notification.trigger.websocket");
27
28
        $port = \Service::getParameter('bzion.features.websocket.pull_port');
29
30
        $fp = @stream_socket_client("tcp://127.0.0.1:" . $port, $errno, $errstr, 1, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT);
31
32
        @fwrite($fp, json_encode(array(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
            'event' => array(
34
                'type' => $channel,
35
                'data' => $message,
36
            )
37
        )) . "\n");
38
39
        // Don't fclose() the connection because of a weird bug with React
40
41
        Debug::finishStopwatch("notification.trigger.websocket");
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public static function isEnabled()
48
    {
49 1
        if (!parent::isEnabled()) {
50 1
            return false;
51
        }
52
53 1
        return \Service::getParameter('bzion.features.websocket.enabled');
54
    }
55
}
56