WebSocketMessagesManager::getSenderData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 $request
23
     * @param ConnectionInterface $from
24
     * @param $error
25
     * @throws WebSocketException
26
     */
27
    function error($request,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
        if(env('APP_DEBUG'))
30
        {
31
            echo 'User error: ';
32
            echo $error."\n";
33
            print_r($request);
34
            echo " ============================================================== \n \n \n";
35
        }
36
37
        $data = [
38
            'event'=>'error',
39
            'message'=>$error
40
        ];
41
        $this->sendToWebSocketUser($from,$data);
42
        throw new WebSocketException();
43
    }
44
45
    /**
46
     * @param ConnectionInterface $conn
47
     * @param $data
48
     */
49
    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...
50
    {
51
        if(!is_array($data))
52
            $data = ['msg'=>$data,'event'=>'default'];
53
54
        if(!isset($data['event']))
55
        {
56
            $data['event'] = 'default';
57
        }
58
59
        if(isset($this->route) && !array_key_exists('sender',$data))
60
            $data['sender'] = $this->getSenderData();
61
62
        $conn->send(json_encode($data));
63
    }
64
65
    /**
66
     * @param int $user_id
67
     * @param array $data
68
     * @return bool
69
     */
70
    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...
71
    {
72
        if(!$this->isOnline($user_id))
73
        {
74
            return false;
75
        }
76
77
        $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...
78
        /** @var ConnectionInterface $conn */
79
        $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...
80
        $this->sendToWebSocketUser($conn,$data);
81
        return true;
82
    }
83
84
    /**
85
     * @param $data
86
     */
87
    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...
88
    {
89
       $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...
90
    }
91
92
    /**
93
     *
94
     * @return array
95
     */
96
    function getSenderData()
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...
97
    {
98
        return [
99
            'id' => \Auth::id(),
100
            'name'=> \Auth::user()->name
101
        ];
102
    }
103
104
    function isOnline($user_id)
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...
105
    {
106
        if(array_key_exists($user_id,$this->userAuthSocketMapper))
107
        {
108
            return true;
109
        }
110
111
        return false;
112
    }
113
114
}