friends   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 1
dl 0
loc 160
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A friends_list() 0 22 3
A requests() 0 23 3
A delete_request() 0 10 3
B approve_request() 0 22 4
B send_request() 0 28 2
A remove_friend() 0 16 4
A set_page_url() 0 4 1
1
<?php
2
3
namespace florinp\messenger\controller;
4
5
use phpbb\template\template;
6
use phpbb\request\request;
7
use phpbb\user;
8
use phpbb\user_loader;
9
use florinp\messenger\models\friends_model;
10
use phpbb\notification\manager;
11
12
class friends
13
{
14
15
	protected $template;
16
	protected $user;
17
	protected $user_loader;
18
	protected $model;
19
	protected $request;
20
	protected $notification_manager;
21
	protected $u_action;
22
23
	public function __construct(
24
		template $template,
25
		request $request,
26
		user $user,
27
		user_loader $user_loader,
28
		friends_model $model,
29
		manager $notification_manager
30
	)
31
	{
32
		$this->template = $template;
33
		$this->request = $request;
34
		$this->user = $user;
35
		$this->user_loader = $user_loader;
36
		$this->model = $model;
37
		$this->notification_manager = $notification_manager;
38
	}
39
40
	public function friends_list()
41
	{
42
		$friends = $this->model->getFriends();
43
		$i = 0;
44
		foreach ($friends as $friend) {
45
			$i = $i + 1;
46
			$this->user_loader->load_users(array(
47
				$friend['user_id']
48
			));
49
			$this->template->assign_block_vars('friends', array(
50
				'user_id' => $friend['user_id'],
51
				'username' => $this->user_loader->get_username($friend['user_id'], 'full'),
52
				'user_colour' => $friend['user_colour'],
53
				'user_status' => ($friend['user_status'] == 1) ? 'online' : 'offline',
54
				'row_count' => $i
55
			));
56
		}
57
58
		$this->template->assign_vars(array(
59
			'U_ACTION' => $this->u_action,
60
		));
61
	}
62
63
	public function requests()
64
	{
65
		$requests = $this->model->get_friends_requests();
66
		$i = 0;
67
		foreach ($requests as $request) {
68
			$i = $i + 1;
69
			$this->user_loader->load_users(array(
70
				$request['user_id'],
71
				$request['sender_id']
72
			));
73
			$this->template->assign_block_vars('requests', array(
74
				'request_id' => $request['request_id'],
75
				'sender_username' => $this->user_loader->get_username($request['sender_id'], 'full'),
76
				'status' => ($request['status'] == 1) ? ($this->user->lang['APPROVED']) : ($this->user->lang['WAITING_FOR_APPROVAL']),
77
				'time' => date('d M Y H:i:s', $request['time']),
78
				'row_count' => $i
79
			));
80
		}
81
82
		$this->template->assign_vars(array(
83
			'U_ACTION' => $this->u_action,
84
		));
85
	}
86
87
	public function delete_request($requests_id)
88
	{
89
		if (is_array($requests_id)) {
90
			foreach ($requests_id as $id) {
91
				$this->model->delete_friend_request($id);
92
			}
93
		} else {
94
			$this->model->delete_friend_request($requests_id);
95
		}
96
	}
97
98
	public function approve_request($requests_id)
99
	{
100
		if (is_array($requests_id)) {
101
			foreach ($requests_id as $id) {
102
				$request_data = $this->model->get_friend_request($id);
103
				if ($request_data) {
104
					$this->model->add_friend(array(
105
						'user_id' => $request_data['user_id'],
106
						'friend_id' => $request_data['sender_id']
107
					));
108
				}
109
110
			}
111
		} else {
112
			$this->model->approve_friend_request($requests_id);
113
			$request_data = $this->model->get_friend_request($requests_id);
114
			$this->model->add_friend(array(
115
				'user_id' => $request_data['user_id'],
116
				'friend_id' => $request_data['sender_id']
117
			));
118
		}
119
	}
120
121
	public function send_request($user_id)
122
	{
123
		$user_id = (int)$user_id;
124
		$sender_id = $this->user->data['user_id'];
125
126
		$insert = array(
127
			'user_id' => $user_id,
128
			'sender_id' => $sender_id
129
		);
130
131
		if ($request_id = $this->model->insert_friends_request($insert)) {
132
133
			$notification_data = array(
134
				'request_id' => $request_id,
135
				'sender_id' => $sender_id,
136
				'sender_username' => $this->user->data ['username'],
137
				'user_id' => $user_id
138
			);
139
140
			$this->notification_manager->add_notifications(array(
141
				'florinp.messenger.notification.type.friend_request'
142
			), $notification_data);
143
144
			return true;
145
		}
146
147
		return false;
148
	}
149
150
	public function remove_friend($user_id)
151
	{
152
153
		if (is_array($user_id)) {
154
			foreach ($user_id as $id) {
155
				$this->model->remove_friend($id);
156
			}
157
			return true;
158
		} else {
159
			if ($user_id > 1) {
160
				$this->model->remove_friend($user_id);
161
				return true;
162
			}
163
		}
164
		return false;
165
	}
166
167
	public function set_page_url($u_action)
168
	{
169
		$this->u_action = $u_action;
170
	}
171
}
172