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

Receiver   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 3
dl 0
loc 129
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A onOpen() 0 8 1
A onMessage() 0 36 5
A onClose() 0 6 1
A onError() 0 7 1
A checkForRequiredInMessage() 0 10 4
A cloneProperties() 0 6 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shanmaseen
5
 * Date: 23/03/19
6
 * Time: 07:40 م
7
 */
8
9
namespace Shamaseen\Laravel\Ratchet;
10
11
12
use function Composer\Autoload\includeFile;
13
use Shamaseen\Laravel\Ratchet\Exceptions\WebSocketException;
14
use Shamaseen\Laravel\Ratchet\Facades\WsRoute;
15
use Shamaseen\Laravel\Ratchet\Objects\Clients\Client;
16
use Shamaseen\Laravel\Ratchet\Routes\Routes;
17
use Ratchet\ConnectionInterface;
18
use Ratchet\MessageComponentInterface;
19
use Shamaseen\Laravel\Ratchet\Traits\WebSocketMessagesManager;
20
21
/**
22
 * Class WebSocket
23
 * @package App\WebSockets
24
 */
25
class Receiver implements MessageComponentInterface
26
{
27
    use WebSocketMessagesManager;
28
29
    /**
30
     * @var Client[]
31
     */
32
    public $clients;
33
    private $routes;
34
    public $userAuthSocketMapper;
35
36
37
    /**
38
     * WebSocket constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->clients = [];
43
        $this->userAuthSocketMapper = [];
44
        WsRoute::mainRoutes();
45
46
        include base_path().'/routes/websocket.php';
47
48
        $this->routes = WsRoute::getRoutes();
49
    }
50
51
    /**
52
     * @param ConnectionInterface $conn
53
     */
54
    public function onOpen(ConnectionInterface $conn) {
55
56
        $this->clients[$conn->resourceId] = new Client();
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
57
        $this->clients[$conn->resourceId]->conn = $conn;
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
58
//        $this->clients[$conn->resourceId]->resourceId = $conn->resourceId;
59
60
        echo "New connection! ({$conn->resourceId})\n";
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
    }
62
63
    /**
64
     * @param ConnectionInterface $from
65
     * @param string $msg
66
     * @throws \Exception
67
     */
68
    public function onMessage(ConnectionInterface $from, $msg) {
69
        try
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after TRY keyword; newline found
Loading history...
70
        {
71
            $msg = json_decode($msg);
72
73
            $this->checkForRequiredInMessage($msg,$from);
74
75
            \Session::setId($msg->session);
76
77
            \Session::start();
78
79
            if($this->routes[$msg->route]->auth && !\Auth::check())
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
80
                $this->error($msg,$from,'Unauthenticated.');
81
            else
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after ELSE keyword; newline found
Loading history...
82
                $this->userAuthSocketMapper[\Auth::id()] = $from->resourceId;
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
83
84
            $class = $this->routes[$msg->route]->controller;
85
            $method = $this->routes[$msg->route]->method;
86
            $controller = new $class;
87
88
            $this->cloneProperties($this,$controller);
89
90
            $controller->conn = $from;
91
92
            if(!method_exists($controller,$method))
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
93
            {
94
                $this->error($msg,$from,'Method doesnt\'t exist !');
95
            }
96
97
            $controller->$method($msg);
98
        }
99
        catch (WebSocketException $exception)
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
100
        {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
101
102
        }
103
    }
104
105
    /**
106
     * @param ConnectionInterface $conn
107
     */
108
    public function onClose(ConnectionInterface $conn) {
109
        // The connection is closed, remove it, as we can no longer send it messages
110
        unset($this->clients[$conn]);
111
112
        echo "Connection {$conn->resourceId} has disconnected\n";
0 ignored issues
show
Bug introduced by
Accessing resourceId on the interface Ratchet\ConnectionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
113
    }
114
115
    /**
116
     * @param ConnectionInterface $conn
117
     * @param \Exception $e
118
     */
119
    public function onError(ConnectionInterface $conn, \Exception $e) {
120
        echo "An error has occurred: {$e->getMessage()}\n";
121
122
        $conn->close();
123
        echo 'end';
124
        die;
125
    }
126
127
    /**
128
     * @param $msg
129
     * @param $from
130
     * @throws WebSocketException
131
     */
132
    function checkForRequiredInMessage($msg,$from)
0 ignored issues
show
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for checkForRequiredInMessage.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
Coding Style introduced by
Expected 1 space between comma and argument "$from"; 0 found
Loading history...
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...
133
    {
134
        if(!isset($msg->route) || !isset($msg->session))
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
135
        {
136
            $this->error($msg,$from,'You can\'t send a request without the route and the session id !');
137
        }
138
139
        if(!isset($this->routes[$msg->route]))
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after IF keyword; 0 found
Loading history...
140
            $this->error($msg,$from,'No such route !');
141
    }
142
143
    /**
144
     * @param $clonedObject
145
     * @param $clone
146
     */
147
    function cloneProperties($clonedObject, $clone)
0 ignored issues
show
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for cloneProperties.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
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...
148
    {
149
        foreach (get_object_vars($clonedObject) as $key => $value) {
150
            $clone->$key = $value;
151
        }
152
    }
153
}