main   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 187
Duplicated Lines 14.97 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 3
dl 28
loc 187
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A handle() 0 3 1
A index() 0 4 1
B publish() 0 31 4
A getFile() 0 7 1
B sendFile() 0 57 7
A load() 10 10 2
A updateMessages() 9 9 2
A checkForNewMessages() 9 9 2
A getFriends() 0 14 1
A getEmoticons() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace florinp\messenger\controller;
4
5
use Symfony\Component\HttpFoundation\Response;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
8
use phpbb\request\request;
9
use phpbb\user;
10
use florinp\messenger\models\main_model;
11
use phpbb\notification\manager;
12
use florinp\messenger\libs\upload;
13
use florinp\messenger\libs\download;
14
15
class main
16
{
17
18
	protected $user;
19
	protected $model;
20
	protected $request;
21
	protected $notification_manager;
22
	protected $upload;
23
	protected $download;
24
25
	public function __construct(
26
		request $request,
27
		user $user,
28
		main_model $model,
29
		manager $notification_manager,
30
		upload $upload,
31
		download $download
32
	)
33
	{
34
		$this->request = $request;
35
		$this->user = $user;
36
		$this->model = $model;
37
		$this->notification_manager = $notification_manager;
38
		$this->upload = $upload;
39
		$this->download = $download;
40
	}
41
42
	public function handle()
43
	{
44
	}
45
46
	public function index()
47
	{
48
49
	}
50
51
	public function publish()
52
	{
53
		$text = $this->request->variable('text', '', true);
54
		$receiver_id = $this->request->variable('receiver_id', 0);
55
		$sender_id = $this->user->data['user_id'];
56
57
		$response = array();
58
		if ($receiver_id != 0 && trim($text) != '') {
59
			$text = htmlspecialchars($text);
60
			$text = str_replace(array("\n", "\r"), '', $text);
61
62
			$message = array(
63
				'sender_id' => $sender_id,
64
				'receiver_id' => $receiver_id,
65
				'text' => $text,
66
				'sentAt' => time()
67
			);
68
69
			if ($id = $this->model->sendMessage($message)) {
70
				$lastMessage = $this->model->getMessageById($id);
71
				$response = array('success' => true, 'message' => $lastMessage);
72
			} else {
73
				$response = array(
74
					'succes' => false,
75
					'error' => 'An error has been ocurred!'
76
				);
77
			}
78
		}
79
80
		return new JsonResponse($response, 200);
81
	}
82
83
	public function getFile($id)
84
	{
85
		$id = explode('_', $id)[1];
86
		$file = $this->model->getFileById($id);
87
		$this->download->setFile($file['file']);
88
		$this->download->sendDownload();
89
	}
90
91
	public function sendFile()
92
	{
93
		$receiver_id = $this->request->variable('receiver_id', 0);
94
		$sender_id = $this->user->data['user_id'];
95
96
		$response = array();
97
		$file = $this->request->file('file');
98
		if ($receiver_id != 0 && !empty($file)) {
99
			if ($file['error'] == 0) {
100
				$this->upload->file($file);
101
				$this->upload->set_allowed_mime_types(array(
102
					'image/gif',
103
					'image/jpeg',
104
					'image/png',
105
					'application/pdf',
106
					'application/x-rar-compressed',
107
					'application/zip',
108
					'application/x-7z-compressed',
109
					'text/plain'
110
				));
111
				$results = $this->upload->upload();
112
				if (isset($results['errors']) && count($results['errors'])) {
113
					$response = array(
114
						'success' => false,
115
						'errors' => $results['errors']
116
					);
117
				} else {
118
					$data = array(
119
						'sender_id' => $sender_id,
120
						'receiver_id' => $receiver_id,
121
						'fileName' => $results['original_filename'],
122
						'file' => $results['filename'],
123
						'type' => $results['mime'],
124
					);
125
					if ($id = $this->model->sendFile($data)) {
126
						$lastFile = $this->model->getFileById($id);
127
						$response = array(
128
							'success' => true,
129
							'file' => $lastFile
130
						);
131
					} else {
132
						$response = array(
133
							'succes' => false,
134
							'error' => 'An error has been ocurred!'
135
						);
136
					}
137
				}
138
			} else {
139
				$response = array(
140
					'succes' => false,
141
					'error' => $file['error']
142
				);
143
			}
144
		}
145
146
		return new JsonResponse($response, 200);
147
	}
148
149 View Code Duplication
	public function load()
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...
150
	{
151
		$friend_id = $this->request->variable('friend_id', 0);
152
153
		if ($friend_id > 0) {
154
			$messages = $this->model->getMessages($friend_id);
155
			return new JsonResponse($messages, 200);
156
		}
157
		return new JsonResponse(array('success' => false, 'error' => 'The request is invalid'), 200);
158
	}
159
160 View Code Duplication
	public function updateMessages()
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...
161
	{
162
		$friend_id = $this->request->variable('friend_id', 0);
163
		if ($friend_id > 0) {
164
			$newVal = $this->model->updateMessagesStatus($friend_id);
165
			return new JsonResponse(array('success' => true, 'newVal' => $newVal), 200);
166
		}
167
		return new JsonResponse(array('success' => false), 200);
168
	}
169
170 View Code Duplication
	public function checkForNewMessages()
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...
171
	{
172
		$friend_id = $this->request->variable('friend_id', 0);
173
		if ($friend_id > 0) {
174
			$messages = $this->model->getInboxFromId($friend_id);
175
			return new JsonResponse(array('success' => true, 'messages' => $messages), 200);
176
		}
177
		return new JsonResponse(array('success' => false), 200);
178
	}
179
180
	public function getFriends()
181
	{
182
		$friends = $this->model->getFriends();
183
		$friends_online = array_filter($friends, function($friend) {
184
			return $friend['user_status'] != 0;
185
		});
186
187
		$response = array(
188
			'friends_online' => count($friends_online),
189
			'friends_list' => $friends
190
		);
191
192
		return new JsonResponse($response, 200);
193
	}
194
195
	public function getEmoticons()
196
	{
197
		$response = array();
198
		return new JsonResponse($response, 200);
199
	}
200
201
}
202