Completed
Push — master ( cd6ca3...800f7e )
by Florin
02:35
created

friends_model::get_friend_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 10

Duplication

Lines 23
Ratio 100 %

Importance

Changes 3
Bugs 3 Features 1
Metric Value
c 3
b 3
f 1
dl 23
loc 23
rs 9.0856
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
namespace florinp\messenger\models;
4
5
use phpbb\config\config;
6
use phpbb\db\driver\driver_interface;
7
use phpbb\user;
8
9
class friends_model
10
{
11
    protected $config;
12
    protected $db;
13
    protected $user;
14
    protected $friends_request_table;
15
    protected $user_friends_table;
16
17 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
18
        config $config,
19
        driver_interface $db,
20
        user $user,
21
        $friends_request_table,
22
        $user_friends_table
23
    )
24
    {
25
        $this->config = $config;
26
        $this->db = $db;
27
        $this->user = $user;
28
        $this->friends_request_table = $friends_request_table;
29
        $this->user_friends_table = $user_friends_table;
30
    }
31
32 View Code Duplication
    public function getFriends()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
33
    {
34
        $sql = "
35
            SELECT u.user_id, 
36
                   u.username, 
37
                   u.username_clean, 
38
                   u.user_type, 
39
                   u.user_colour, 
40
                   s.session_id, 
41
                   s.session_time
42
            FROM " . $this->user_friends_table . "
43
            LEFT JOIN " . USERS_TABLE . " AS u ON u.user_id = " . $this->user_friends_table . ".friend_id
44
            LEFT JOIN " . SESSIONS_TABLE . " AS s ON s.session_user_id = u.user_id
45
            WHERE " . $this->user_friends_table . ".user_id = " . (int)$this->user->data['user_id'] . "
46
            GROUP BY u.user_id
47
        ";
48
        $result = $this->db->sql_query($sql);
49
50
        $friends = array();
51
        while ($row = $this->db->sql_fetchrow($result)) {
52
            $friends[] = array(
53
                'user_id' => $row['user_id'],
54
                'username' => $row['username_clean'],
55
                'user_colour' => $row['user_colour'],
56
                'user_status' => ($row['session_time'] >= (time() - ($this->config['load_online_time'] * 60))) ? 1 : 0,
57
            );
58
        }
59
        $this->db->sql_freeresult();
60
61
        return $friends;
62
    }
63
64
    public function get_friends_requests()
65
    {
66
67
        $requests = array();
68
69
        $sql = "
70
			SELECT `request_id`,
71
					`user_id`,
72
					`sender_id`,
73
					`status`,
74
					`time`
75
			FROM " . $this->friends_request_table . "
76
			WHERE `user_id` = " . (int)$this->user->data['user_id'] . "
77
                    AND `status` = 0
78
			ORDER BY `time` DESC
79
		";
80
81
        $result = $this->db->sql_query($sql);
82
83
        while ($row = $this->db->sql_fetchrow($result)) {
84
            $requests[] = $row;
85
        }
86
87
        return $requests;
88
    }
89
90 View Code Duplication
    public function get_friend_request($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
91
    {
92
        $sql = "
93
			SELECT `request_id`,
94
					`user_id`,
95
					`sender_id`,
96
					`status`,
97
					`time`
98
			FROM " . $this->friends_request_table . "
99
			WHERE `request_id` = " . (int)$id . "
100
                AND `status` = 0
101
			ORDER BY `time` DESC
102
			LIMIT 1
103
		";
104
        $result = $this->db->sql_query($sql);
105
        $row = $this->db->sql_fetchrow($result);
106
107
        if ($this->approve_friend_request($id)) {
108
            return $row;
109
        } else {
110
            return false;
111
        }
112
    }
113
114 View Code Duplication
    public function get_request_by_sender_id($sender_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
115
    {
116
        $sql = "
117
            SELECT `request_id`,
118
					`user_id`,
119
					`sender_id`,
120
					`status`,
121
					`time`
122
			FROM " . $this->friends_request_table . "
123
			WHERE `sender_id` = " . (int)$sender_id . "
124
                AND `status` = 0
125
			ORDER BY `time` DESC
126
			LIMIT 1 
127
        ";
128
        $result = $this->db->sql_query($sql);
129
        $row = $this->db->sql_fetchrow($result);
130
131
        return $row;
132
    }
133
134
    public function insert_friends_request(array $data)
135
    {
136
        $sql = "
137
      INSERT INTO " . $this->friends_request_table . "
138
        (
139
          `user_id`,
140
          `sender_id`,
141
          `status`,
142
          `time`
143
        )
144
      VALUES
145
        (
146
          " . ( int )$data ['user_id'] . ",
147
          " . ( int )$data ['sender_id'] . ",
148
          0,
149
          " . time() . "
150
        )
151
    ";
152
        $this->db->sql_query($sql);
153
154
        return $this->db->sql_nextid();
155
    }
156
157 View Code Duplication
    public function delete_friend_request($request_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
158
    {
159
        $sql = "
160
			DELETE FROM " . $this->friends_request_table . " WHERE `request_id` = " . (int)$request_id . "
161
		";
162
163
        return $this->db->sql_query($sql);
164
    }
165
166 View Code Duplication
    public function approve_friend_request($request_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
167
    {
168
        $sql = "
169
			UPDATE " . $this->friends_request_table . " SET `status` = 1 WHERE `request_id` = " . (int)$request_id . "
170
		";
171
172
        return $this->db->sql_query($sql);
173
    }
174
175
    public function add_friend($data)
176
    {
177
178
        $check_friend = $this->check_friend($data);
179
        if ($check_friend == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
180
            $sql = "
181
				INSERT INTO " . $this->user_friends_table . "
182
					(
183
						`user_id`,
184
						`friend_id`
185
					)
186
				VALUES
187
					(
188
						" . (int)$data['user_id'] . ",
189
						" . (int)$data['friend_id'] . "
190
					)
191
			";
192
            if ($this->db->sql_query($sql)) {
193
                $aux = $data['user_id'];
194
                $data['user_id'] = $data['friend_id'];
195
                $data['friend_id'] = $aux;
196
197
                self::add_friend($data);
198
            } else {
199
                return false;
200
            }
201
        } else {
202
            return false;
203
        }
204
205
    }
206
207 View Code Duplication
    public function check_friend($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
208
    {
209
        $sql = "
210
			SELECT COUNT(*) AS `count`
211
			FROM " . $this->user_friends_table . "
212
			WHERE `user_id` = " . (int)$data['user_id'] . "
213
			 		AND `friend_id` = " . (int)$data['friend_id'] . "
214
		";
215
        $this->db->sql_query($sql);
216
        $count = $this->db->sql_fetchfield('count');
217
        if ($count > 0) {
218
            return true;
219
        } else {
220
            return false;
221
        }
222
    }
223
224 View Code Duplication
    public function check_request($data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
225
    {
226
        $sql = "
227
			SELECT COUNT(*) AS `count`
228
			FROM " . $this->friends_request_table . "
229
			WHERE `user_id` = " . (int)$data['user_id'] . "
230
					AND `sender_id` = " . (int)$data['sender_id'] . "
231
					AND `status` = 0
232
			LIMIT 1
233
		";
234
        $this->db->sql_query($sql);
235
        $count = $this->db->sql_fetchfield('count');
236
        if ($count > 0) {
237
            return true;
238
        } else {
239
            return false;
240
        }
241
    }
242
243
    public function remove_friend($user_id)
244
    {
245
        $sql = "DELETE FROM " . $this->user_friends_table . " WHERE `user_id` = " . (int)$user_id . "";
246
        $this->db->sql_query($sql);
247
248
        $sql = "DELETE FROM " . $this->user_friends_table . " WHERE `friend_id` = " . (int)$user_id . "";
249
        $this->db->sql_query($sql);
250
    }
251
252
}
253