Completed
Push — master ( da46fb...91cc27 )
by Ankit
09:18
created

Chat::onMessage()   C

Complexity

Conditions 10
Paths 10

Size

Total Lines 93
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 58
nc 10
nop 2
dl 0
loc 93
rs 5.0515
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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