Completed
Push — master ( d40969...da46fb )
by Ankit
08:16
created

Chat   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 166
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A onOpen() 0 6 1
A setID() 0 8 1
D onMessage() 0 95 10
A onSidebar() 0 5 1
A onConversation() 0 5 1
A onReceiver() 0 5 1
A onSearch() 0 5 1
A onCompose() 0 5 1
A onReply() 0 5 1
A onClose() 0 5 1
A onError() 0 4 1
1
<?php
2
namespace ChatApp;
3
use Ratchet\MessageComponentInterface;
4
use Ratchet\ConnectionInterface;
5
use ChatApp\Models\Message;
6
use ChatApp\Reply;
7
use ChatApp\Conversation;
8
use ChatApp\Receiver;
9
use ChatApp\SideBar;
10
use ChatApp\Search;
11
use ChatApp\Compose;
12
use ChatApp\Online;
13
14
class Chat implements MessageComponentInterface {
15
    protected $clients;
16
17
    public function __construct() {
18
        $this->clients = new \SplObjectStorage;
19
    }
20
21
    public function onOpen(ConnectionInterface $conn) {
22
        $conn = $this->setID($conn);
23
        $this->clients->attach($conn);
24
        echo "New connection! ({$conn->resourceId})\n";
25
        Online::setOnlineStatus($conn->userId);
26
    }
27
28
    public function setID($conn)
0 ignored issues
show
Coding Style introduced by
setID uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
        session_id($conn->WebSocket->request->getCookies()['PHPSESSID']);
31
        @session_start();
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...
32
        $conn->userId = $_SESSION['start'];
33
        session_write_close();
34
        return $conn;
35
    }
36
37
    public function onMessage(ConnectionInterface $from, $msg) {
38
        $sessionId = $from->WebSocket->request->getCookies()['PHPSESSID'];
0 ignored issues
show
Bug introduced by
Accessing WebSocket 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...
Unused Code introduced by
$sessionId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
        if($msg == 'OpenChat initiated..!')
40
        {
41
            $initial = (object) array();
42
            $initial->initial = json_decode($this->onSidebar($from->userId));
0 ignored issues
show
Bug introduced by
Accessing userId 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...
43
44
            if($initial->initial != null) {
45
                $initial->conversation = json_decode(
46
                    $this->onConversation(
47
                        json_encode([
48
                            "username" => $initial->initial[0]->login_id,
49
                            "load" => 10,
50
                            "userId" => $from->userId
0 ignored issues
show
Bug introduced by
Accessing userId 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...
51
                        ]), True
52
                    )
53
                );
54
            }
55
            $from->send(json_encode($initial));
56
        }
57
        elseif ($msg == 'Load Sidebar')
58
        {
59
            $sidebar = (object) array();
60
            $sidebar->sidebar = json_decode($this->onSidebar($from->userId));
0 ignored issues
show
Bug introduced by
Accessing userId 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
            $from->send(json_encode($sidebar));
62
        }
63
        elseif (@json_decode($msg)->newConversation == 'Initiated')
64
        {
65
            $msg = json_decode($msg);
66
            $msg->userId = $from->userId;
0 ignored issues
show
Bug introduced by
Accessing userId 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...
67
            $result = (object) array();
68
            $result->conversation = json_decode($this->onConversation(json_encode($msg), False));
69
            $from->send(json_encode($result));
70
        }
71
        elseif (@json_decode($msg)->search == 'search')
72
        {
73
            $msg = json_decode($msg);
74
            $msg->userId = $from->userId;
0 ignored issues
show
Bug introduced by
Accessing userId 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...
75
            $searchResult = $this->onSearch($msg);
76
            $from->send($searchResult);
77
        }
78
        elseif (@json_decode($msg)->Compose == 'Compose')
79
        {
80
            $msg = json_decode($msg);
81
            $msg->userId = $from->userId;
0 ignored issues
show
Bug introduced by
Accessing userId 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...
82
            $composeResult = $this->onCompose($msg);
83
            $from->send($composeResult);
84
        }
85
        else
86
        {
87
            $msg = (object) json_decode($msg);
88
            $msg->userId = $from->userId;
0 ignored issues
show
Bug introduced by
Accessing userId 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...
89
            $getReturn = $this->onReply($msg);
90
            echo $getReturn;
91
92
            $receiveResult = (object) array();
93
            $sentResult = (object) array();
94
            foreach ($this->clients as $client)
95
            {
96
                if ($client->userId == $msg->name)
97
                {
98
                    $receiveResult->sidebar = json_decode($this->onSidebar($client->userId));
99
100
                    $receiveResult->reply = json_decode(
101
                        $this->onReceiver(
102
                            json_encode([
103
                                "username" => $client->userId,
104
                                "load" => 10,
105
                                "userId" => $from->userId
0 ignored issues
show
Bug introduced by
Accessing userId 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...
106
                            ]), True
107
                        )
108
                    );
109
110
                    $client->send(json_encode($receiveResult));
111
                }
112
                elseif($client == $from)
113
                {
114
                    $sentResult->sidebar = json_decode($this->onSidebar($client->userId));
115
116
                    $sentResult->conversation = json_decode(
117
                        $this->onConversation(
118
                            json_encode([
119
                                "username" => $msg->name,
120
                                "load" => 10,
121
                                "userId" => $from->userId
0 ignored issues
show
Bug introduced by
Accessing userId 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...
122
                            ]), True
123
                        )
124
                    );
125
126
                    $client->send(json_encode($sentResult));
127
                }
128
            }
129
130
        }
131
    }
132
133
    public function onSidebar($data)
134
    {
135
        $obSidebar = new Sidebar();
136
        return $obSidebar->loadSideBar($data);
137
    }
138
139
    public function onConversation($data, $para)
140
    {
141
        $obConversation = new Conversation();
142
        return $obConversation->conversationLoad($data, $para);
143
    }
144
145
    public function onReceiver($data, $para)
146
    {
147
        $obReceiver = new Receiver();
148
        return $obReceiver->receiverLoad($data, $para);
149
    }
150
151
    public function onSearch($data)
152
    {
153
        $obSearch = new Search();
154
        return $obSearch->searchItem($data);
155
    }
156
157
    public function onCompose($data)
158
    {
159
        $obCompose = new Compose();
160
        return $obCompose->selectUser($data);
161
    }
162
163
    public function onReply($data)
164
    {
165
        $obReply = new Reply();
166
        return $obReply->replyTo($data);
167
    }
168
169
    public function onClose(ConnectionInterface $conn) {
170
        Online::removeOnlineStatus($conn->userId);
0 ignored issues
show
Bug introduced by
Accessing userId 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...
171
        $this->clients->detach($conn);
172
        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...
173
    }
174
    public function onError(ConnectionInterface $conn, \Exception $e) {
175
        echo "An error has occurred: {$e->getMessage()}\n";
176
        $conn->close();
177
    }
178
179
}