1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Compose 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
|
|
|
require_once dirname(__DIR__).'/vendor/autoload.php'; |
16
|
|
|
use mysqli; |
17
|
|
|
use Dotenv\Dotenv; |
18
|
|
|
$dotenv = new Dotenv(dirname(__DIR__)); |
19
|
|
|
$dotenv->load(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* To Compose New Message |
23
|
|
|
* |
24
|
|
|
* @category PHP |
25
|
|
|
* @package OpenChat |
26
|
|
|
* @author Ankit Jain <[email protected]> |
27
|
|
|
* @license The MIT License (MIT) |
28
|
|
|
* @link https://github.com/ankitjain28may/openchat |
29
|
|
|
*/ |
30
|
|
|
class Compose |
31
|
|
|
{ |
32
|
|
|
/* |
33
|
|
|
|-------------------------------------------------------------------------- |
34
|
|
|
| Compose Class |
35
|
|
|
|-------------------------------------------------------------------------- |
36
|
|
|
| |
37
|
|
|
| To Compose New Message. |
38
|
|
|
| |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
protected $connect; |
42
|
|
|
protected $array; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new class instance. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function __construct() |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->connect = new mysqli( |
52
|
|
|
getenv('DB_HOST'), |
53
|
|
|
getenv('DB_USER'), |
54
|
|
|
getenv('DB_PASSWORD'), |
55
|
|
|
getenv('DB_NAME') |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->array = array(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fetch User from the DB |
63
|
|
|
* |
64
|
|
|
* @param object $msg To store user id and suggestion value |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function selectUser($msg) |
69
|
|
|
{ |
70
|
|
|
$userId = $msg->userId; |
71
|
|
|
$suggestion = $msg->value; |
72
|
|
|
$suggestion = trim($suggestion); |
73
|
|
|
if (!empty($userId) && !empty($suggestion)) { |
74
|
|
|
$query = "SELECT * FROM login where login_id != '$userId' and |
75
|
|
|
name like '$suggestion%' ORDER BY name DESC"; |
76
|
|
|
if ($result = $this->connect->query($query)) { |
77
|
|
|
if ($result->num_rows > 0) { |
78
|
|
|
while ($row = $result->fetch_assoc()) { |
79
|
|
|
$row["login_id"] = bin2hex( |
80
|
|
|
convert_uuencode($row["login_id"]) |
81
|
|
|
); |
82
|
|
|
$this->array = array_merge($this->array, [$row]); |
83
|
|
|
} |
84
|
|
|
$this->array = array_merge([], ["Compose" => $this->array]); |
85
|
|
|
return json_encode($this->array); |
86
|
|
|
} |
87
|
|
|
return json_encode(["Compose" => "Not Found"]); |
88
|
|
|
} |
89
|
|
|
return json_encode(["Compose" => "Query Failed"]); |
90
|
|
|
} |
91
|
|
|
return json_encode(["Compose" => "Not Found"]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.