Chat   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onOpen() 0 7 1
A setID() 0 8 1
C onMessage() 0 90 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 6 1
A onError() 0 5 1
1
<?php
2
/**
3
 * Chat Class Doc Comment
4
 *
5
 * PHP version 5
6
 *
7
 * @category PHP
8
 * @package  OpenChat
9
 * @author   Ankit Jain <[email protected]>
10
 * @license  The MIT License (MIT)
11
 * @link     https://github.com/ankitjain28may/openchat
12
 */
13
namespace ChatApp;
14
15
use Ratchet\MessageComponentInterface;
16
use Ratchet\ConnectionInterface;
17
// use ChatApp\Models\Message;
18
use ChatApp\Reply;
19
use ChatApp\Conversation;
20
use ChatApp\Receiver;
21
use ChatApp\SideBar;
22
use ChatApp\Search;
23
use ChatApp\Compose;
24
use ChatApp\Online;
25
26
/**
27
 * This Class handles the all the main functionalities for this ChatApp.
28
 *
29
 * @category PHP
30
 * @package  OpenChat
31
 * @author   Ankit Jain <[email protected]>
32
 * @license  The MIT License (MIT)
33
 * @link     https://github.com/ankitjain28may/openchat
34
 */
35
36
class Chat implements MessageComponentInterface
37
{
38
    /*
39
    |--------------------------------------------------------------------------
40
    | Chat Class
41
    |--------------------------------------------------------------------------
42
    |
43
    | This Class handles the all the main functionalities for this ChatApp.
44
    |
45
    */
46
47
    protected $clients;
48
49
    /**
50
     * Create a new class instance.
51
     *
52
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
53
     */
54
    public function __construct()
55
    {
56
        $this->clients = new \SplObjectStorage;
57
    }
58
59
    /**
60
     * Open the Socket Connection and get client connection
61
     *
62
     * @param ConnectionInterface $conn To store client details
63
     *
64
     * @return void
65
     */
66
    public function onOpen(ConnectionInterface $conn)
67
    {
68
        $conn = $this->setID($conn);
69
        $this->clients->attach($conn);
70
        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...
71
        Online::setOnlineStatus($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...
72
    }
73
74
    /**
75
     * Set Session Id in Connection object
76
     *
77
     * @param ConnectionInterface $conn To store client details
78
     *
79
     * @return $conn
0 ignored issues
show
Documentation introduced by
The doc-type $conn could not be parsed: Unknown type name "$conn" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
80
     */
81
    public function setID($conn)
82
    {
83
        session_id($conn->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...
84
        @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...
85
        $conn->userId = $_SESSION['start'];
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...
86
        session_write_close();
87
        return $conn;
88
    }
89
90
    /**
91
     * Send Messages to Clients
92
     *
93
     * @param ConnectionInterface $from To store client details
94
     * @param string              $msg  To store message
95
     *
96
     * @return void
97
     */
98
    public function onMessage(ConnectionInterface $from, $msg)
99
    {
100
        $msg = (object)json_decode($msg);
101
        if ($msg->type == 'OpenChat initiated..!') {
102
            $initial = (object)array();
103
            $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...
104
105
            if ($initial->initial != null) {
106
                $initial->conversation = json_decode(
107
                    $this->onConversation(
108
                        json_encode(
109
                            [
110
                            "details" => $initial->initial[0]->login_id,
111
                            "load" => 20,
112
                            "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...
113
                            ]
114
                        ), true
115
                    )
116
                );
117
            }
118
            $from->send(json_encode($initial));
119
        } else if ($msg->type == 'Load Sidebar') {
120
            $sidebar = (object)array();
121
            $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...
122
            $from->send(json_encode($sidebar));
123
        } else if ($msg->type == 'Initiated') {
124
            $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...
125
            $result = (object)array();
126
            $result->conversation = json_decode(
127
                $this->onConversation(json_encode($msg), false)
128
            );
129
            $from->send(json_encode($result));
130
        } else if ($msg->type == 'Search') {
131
            $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...
132
            $searchResult = $this->onSearch($msg);
133
            $from->send($searchResult);
134
        } else if ($msg->type == 'Compose') {
135
            $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...
136
            $composeResult = $this->onCompose($msg);
137
            $from->send($composeResult);
138
        } else {
139
            $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...
140
            $msg->name = convert_uudecode(hex2bin($msg->name));
141
142
            $getReturn = $this->onReply($msg);
143
            echo $getReturn;
144
145
            $receiveResult = (object)array();
146
            $sentResult = (object)array();
147
            foreach ($this->clients as $client) {
148
                if ($client->userId == $msg->name) {
149
                    $receiveResult->sidebar = json_decode(
150
                        $this->onSidebar($client->userId)
151
                    );
152
153
                    $receiveResult->reply = json_decode(
154
                        $this->onReceiver(
155
                            json_encode(
156
                                [
157
                                "details" => $client->userId,
158
                                "load" => 20,
159
                                "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...
160
                                ]
161
                            ), true
162
                        )
163
                    );
164
165
                    $client->send(json_encode($receiveResult));
166
                } else if ($client == $from) {
167
                    $sentResult->sidebar = json_decode(
168
                        $this->onSidebar($client->userId)
169
                    );
170
171
                    $sentResult->conversation = json_decode(
172
                        $this->onConversation(
173
                            json_encode(
174
                                [
175
                                "details" => bin2hex(convert_uuencode($msg->name)),
176
                                "load" => 20,
177
                                "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...
178
                                ]
179
                            ), true
180
                        )
181
                    );
182
                    $client->send(json_encode($sentResult));
183
                }
184
            }
185
186
        }
187
    }
188
189
    /**
190
     * To Call SideBar Class
191
     *
192
     * @param string $data To store data
193
     *
194
     * @return string
195
     */
196
    public function onSidebar($data)
197
    {
198
        $obSidebar = new SideBar();
199
        return $obSidebar->loadSideBar($data);
200
    }
201
202
    /**
203
     * To Call Conversation Class
204
     *
205
     * @param string  $data to store data
206
     * @param boolean $para to store True/False
207
     *
208
     * @return string
209
     */
210
    public function onConversation($data, $para)
211
    {
212
        $obConversation = new Conversation();
213
        return $obConversation->conversationLoad($data, $para);
214
    }
215
216
    /**
217
     * To Call Receiver Class
218
     *
219
     * @param string  $data to store data
220
     * @param boolean $para to store True/False
221
     *
222
     * @return string
223
     */
224
    public function onReceiver($data, $para)
225
    {
226
        $obReceiver = new Receiver();
227
        return $obReceiver->receiverLoad($data, $para);
228
    }
229
230
    /**
231
     * To Call Search Class
232
     *
233
     * @param string $data to store data
234
     *
235
     * @return string
236
     */
237
    public function onSearch($data)
238
    {
239
        $obSearch = new Search();
240
        return $obSearch->searchItem($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
241
    }
242
243
    /**
244
     * To Call Compose Class
245
     *
246
     * @param string $data to store data
247
     *
248
     * @return string
249
     */
250
    public function onCompose($data)
251
    {
252
        $obCompose = new Compose();
253
        return $obCompose->selectUser($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
254
    }
255
256
    /**
257
     * To Call Reply Class
258
     *
259
     * @param string $data to store data
260
     *
261
     * @return string
262
     */
263
    public function onReply($data)
264
    {
265
        $obReply = new Reply();
266
        return $obReply->replyTo($data);
0 ignored issues
show
Documentation introduced by
$data is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
267
    }
268
269
    /**
270
     * To Call Online Class
271
     *
272
     * @param ConnectionInterface $conn To store client details
273
     *
274
     * @return void
275
     */
276
    public function onClose(ConnectionInterface $conn)
277
    {
278
        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...
279
        $this->clients->detach($conn);
280
        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...
281
    }
282
283
    /**
284
     * To Show error due to any problem occured
285
     *
286
     * @param ConnectionInterface $conn To store client details
287
     * @param \Exception          $e    To store exception
288
     *
289
     * @return void
290
     */
291
    public function onError(ConnectionInterface $conn, \Exception $e)
292
    {
293
        echo "An error has occurred: {$e->getMessage()}\n";
294
        $conn->close();
295
    }
296
297
}
298