Completed
Push — master ( 8bad15...736b77 )
by Florin
02:26
created

main::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
194
        $eicons = array();
195
        foreach ($emoticons as $code => $value) {
196
            $eicons[$value] = $code;
197
        }
198
199
        $response = array();
200
        foreach ($eicons as $emoticon) {
201
            $item = array();
202
            $item['code'] = $emoticon;
203
            $item['image'] = $this->emojione->toImage($emoticon);
204
205
            $response[] = $item;
206
        }
207
208
        return new JsonResponse($response, 200);
209
    }
210
211
}
212