Completed
Push — master ( 9edb5e...d40969 )
by Ankit
03:51
created

Compose::selectUser()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 5
nop 1
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace ChatApp;
4
require_once (dirname(__DIR__) . '/vendor/autoload.php');
5
use ChatApp\Session;
6
use Dotenv\Dotenv;
7
$dotenv = new Dotenv(dirname(__DIR__));
8
$dotenv->load();
9
10
11
/**
12
*
13
*/
14
class Compose
15
{
16
    protected $connect;
17
    protected $array;
18
19
    public function __construct()
20
    {
21
        $this->connect = mysqli_connect(
22
            getenv('DB_HOST'),
23
            getenv('DB_USER'),
24
            getenv('DB_PASSWORD'),
25
            getenv('DB_NAME')
26
        );
27
28
        $this->array = array();
29
    }
30
31
    public function selectUser($msg)
32
    {
33
        $userId = $msg->userId;
34
        $suggestion = $msg->value;
35
        $suggestion = trim($suggestion);
36
        if(!empty($userId) && !empty($suggestion))
37
        {
38
            $query = "SELECT * FROM login where login_id != '$userId' and name like '$suggestion%' ORDER BY name DESC";
39
            if($result = $this->connect->query($query))
40
            {
41
                if($result->num_rows > 0)
42
                {
43
                    while($row = $result->fetch_assoc())
44
                    {
45
                        $this->array = array_merge($this->array, [$row]);
46
                    }
47
                    $this->array = array_merge([], ["Compose" => $this->array]);
48
                    return json_encode($this->array);
49
                }
50
                return json_encode(["Compose" => "Not Found"]);
51
            }
52
            return json_encode(["Compose" => "Query Failed"]);
53
        }
54
        return json_encode(["Compose" => "Not Found"]);
55
    }
56
}
57
58