WebSocketAdapter::trigger()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 10
cp 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
crap 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