Completed
Push — master ( 3bdd73...3b96a7 )
by Ankit
02:28
created

Reply::updateMessages()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 5
dl 0
loc 16
rs 9.4285
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__) . '/config/database.php');
5
use ChatApp\Session;
6
use Dotenv\Dotenv;
7
$dotenv = new Dotenv(dirname(__DIR__));
8
$dotenv->load();
9
10
11
/**
12
* Store Message in the Database
13
*/
14
class Reply
15
{
16
    protected $connect;
17
18
    public function __construct($sessionId)
19
    {
20
        session_id($sessionId);
21
        @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...
22
        $this->connect = mysqli_connect(
23
            getenv('DB_HOST'),
24
            getenv('DB_USER'),
25
            getenv('DB_PASSWORD'),
26
            getenv('DB_NAME')
27
        );
28
        session_write_close();
29
    }
30
31
    public function replyTo($msg)
32
    {
33
34
        if(Session::get('start') != null && isset($msg))  //checks for session login and the value send
35
        {
36
            $userId = Session::get('start');
37
            $msg = json_decode($msg);   //decode json value
38
            $identifier = $msg->name;
39
40
            $receiverID = $identifier;  //stores id of the person whom message is to be sent
41
42 View Code Duplication
            if($identifier > $userId)    // geneate specific unique code to store messages
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
43
                $identifier = $userId . ":" . $identifier;
44
            else
45
                $identifier = $identifier . ":" . $userId;
46
47
            $reply = addslashes(trim($msg->reply[0])); // stores the message sent by the user.
48
49
            $time = date("D d M Y H:i:s", time() + 16200);  // current time
50
            $time_id = date("YmdHis", time() + 16200); //to sort the array on the basis of time
51
52
            //the sender id must not be equal to current session id
53
            if($reply != "" && $receiverID != $userId)
54
            {
55
                // check whether the receiver is authorized or registered
56
                $query = "SELECT * from login where login_id = '$receiverID'";
57
58
                $result = $this->connect->query($query);
59
                if($result->num_rows > 0)     // if true
60
                {
61
                    //check whether he is sending message for thr first time or he has sent messages before
62
                    $query = "SELECT * from total_message where identifier = '$identifier'";
63
                    $result = $this->connect->query($query);
64
                    if($result->num_rows>0)               // if he has sent messages before
65
                    {
66
                        // Update Total_Message Table
67
                        $query = "UPDATE total_message SET total_messages = total_messages+1, time = '$time', unread = 1, id = '$time_id' WHERE identifier = '$identifier'";
68
                        $this->updateMessages($query, $identifier, $reply, $userId, $time);
69
70
                    }
71
                    else    // if he sends message for the first time
72
                    {
73
                        $length = strlen($userId);
74
                        if(substr($identifier, 0, $length) == $userId) // generate specific unique code
75
                        {
76
                            $user2 = substr($identifier, $length+1);
77
                            $user1 = $userId;
78
                        }
79
                        else
80
                        {
81
                            $user2 = $userId;
82
                            $length = strlen($identifier) - $length-1;
83
                            $user1 = substr($identifier, 0, $length);
84
                        }
85
                        // insert Details in Total_Message Table
86
                        $query = "INSERT into total_message values('$identifier', 1, '$user1', '$user2', 1, '$time', '$time_id')";
87
                        $this->updateMessages($query, $identifier, $reply, $userId, $time);
88
                    }
89
                }
90
                else // if he is unauthorized echo message is failed
91
                {
92
                    echo "Message is failed";
93
                }
94
            }
95
        }
96
        else
97
        {
98
            echo "failed";
99
        }
100
        $this->connect->close();
101
    }
102
103
    public function updateMessages($query, $identifier, $reply, $userId, $time)
104
    {
105
        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...
106
        {
107
            //insert message in db
108
            $query = "INSERT into messages values('$identifier', '$reply', '$userId', '$time', null)";
109
            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...
110
            {
111
                echo "Messages is sent \n";    // if query is executed return true
112
            }
113
            else
114
            {
115
                echo "Message is failed";
116
            }
117
        }
118
    }
119
120
121
}
122