Completed
Push — master ( d60caa...394d07 )
by Ankit
02:48
created

Reply::replyTo()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 9
nop 1
dl 0
loc 50
rs 6.7272
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 13 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 Dotenv\Dotenv;
6
$dotenv = new Dotenv(dirname(__DIR__));
7
$dotenv->load();
8
9
10
/**
11
* Store Message in the Database
12
*/
13
class Reply
14
{
15
    protected $connect;
16
17
    public function __construct()
18
    {
19
        $this->connect = mysqli_connect(
20
            getenv('DB_HOST'),
21
            getenv('DB_USER'),
22
            getenv('DB_PASSWORD'),
23
            getenv('DB_NAME')
24
        );
25
    }
26
27
    public function replyTo($msg)
28
    {
29
        if (!empty($msg)) {
30
            //checks for the value send
31
            $userId = $msg->userId;
32
            $receiverID = $msg->name; //stores id of the person whom message is to be sent
33
            $identifier = "";
1 ignored issue
show
Unused Code introduced by
$identifier 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...
34
35
            if ($receiverID > $userId) {
36
                // geneate specific unique code to store messages
37
                $user1 = $userId;
38
                $user2 = $receiverID;
39
                $identifier = $userId . ":" . $receiverID;
40
            } else {
41
                $user1 = $receiverID;
42
                $user2 = $userId;
43
                $identifier = $receiverID . ":" . $userId;
44
            }
45
46
            $reply = addslashes(trim($msg->reply)); // stores the message sent by the user.
47
48
            $time = date("D d M Y H:i:s", time() + 16200);  // current time
49
            $time_id = date("YmdHis", time() + 16200); //to sort the array on the basis of time
50
51
            //the sender id must not be equal to current session id
52
            if ($reply != "" && $receiverID != $userId) {
53
                // check whether the receiver is authorized or registered
54
                $query = "SELECT * from login where login_id = '$receiverID'";
55
56
                $result = $this->connect->query($query);
57
                if ($result->num_rows > 0) {
58
                    //check whether he is sending message for thr first time or he has sent messages before
59
                    $query = "SELECT * from total_message where identifier = '$identifier'";
60
                    $result = $this->connect->query($query);
61
                    if ($result->num_rows > 0) {
62
                        // if he has sent messages before Update Total_Message Table
63
                        $query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'";
64
                        return $this->updateMessages($query, $identifier, $reply, $userId, $time);
65
66
                    } else {
67
                        // if he sends message for the first time insert Details in Total_Message Table
68
                        $query = "INSERT into total_message values('$identifier', 1, '$user1', '$user2', 1, '$time', '$time_id')";
69
                        return $this->updateMessages($query, $identifier, $reply, $userId, $time);
70
                    }
71
                }
72
                return "Invalid Authentication";  // if he is unauthorized echo message is failed
73
            }
74
        }
75
        return "Failed";
76
    }
77
78
    public function updateMessages($query, $identifier, $reply, $userId, $time)
79
    {
80
        if ($result = $this->connect->query($query)) {
0 ignored issues
show
Unused Code introduced by
$result 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...
81
            //insert message in db
82
            $query = "INSERT into messages values('$identifier', '$reply', '$userId', '$time', null)";
83
            if ($this->connect->query($query)) {
84
                return "Messages is sent";    // if query is executed return true
85
            }
86
            return "Message is failed";
87
        }
88
    }
89
90
91
}
92
93