Passed
Push — master ( f55dd4...6ac744 )
by Mohammad
13:39 queued 20s
created

WebSocketMessagesManager::sendToWebSocketUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shanmaseen
5
 * Date: 23/03/19
6
 * Time: 10:15 م
7
 */
8
9
namespace Shamaseen\Laravel\Ratchet\Traits;
10
11
12
use Shamaseen\Laravel\Ratchet\Exceptions\WebSocketException;
13
use Ratchet\ConnectionInterface;
14
15
/**
16
 * Trait WebSocketMessagesManager
17
 * @package App\WebSockets
18
 */
19
trait WebSocketMessagesManager
20
{
21
    /**
22
     * @param $msg
23
     * @param $from
24
     * @param $error
25
     * @throws WebSocketException
26
     */
27
    function error($msg,ConnectionInterface $from, $error)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29
        echo 'Error: ';
30
        echo $error."\n";
31
        print_r($msg);
32
        $data = [
33
            'type'=>'error',
34
            'message'=>$error
35
        ];
36
        $this->sendToWebSocketUser($from,$data);
37
        throw new WebSocketException();
38
    }
39
40
    /**
41
     * @param ConnectionInterface $conn
42
     * @param $data
43
     */
44
    function sendToWebSocketUser(ConnectionInterface $conn,$data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
    {
46
        $conn->send(json_encode($data));
47
    }
48
49
    /**
50
     * @param int $user_id
51
     * @param array $data
52
     */
53
    function sendToUser($user_id,$data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style Naming introduced by
The variable $user_id is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
54
    {
55
        $resourceId = $this->userAuthSocketMapper[$user_id];
0 ignored issues
show
Bug introduced by
The property userAuthSocketMapper does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
56
        /** @var ConnectionInterface $conn */
57
        $conn = $this->clients[$resourceId]->conn;
0 ignored issues
show
Bug introduced by
The property clients does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
        $this->sendToWebSocketUser($conn,$data);
59
    }
60
61
    /**
62
     * @param $data
63
     */
64
    function sendBack($data)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
       $this->sendToWebSocketUser($this->conn,$data);
0 ignored issues
show
Bug introduced by
The property conn does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
    }
68
69
}