Completed
Push — master ( f88301...361860 )
by Alexey
05:09
created

ChatsController::sendFormAction()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 32
Code Lines 26

Duplication

Lines 10
Ratio 31.25 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 32
rs 6.7272
cc 7
eloc 26
nc 16
nop 1
1
<?php
2
3
/**
4
 * Chat Controller
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class ChatsController extends Controller
12
{
13
    public function eventsAction($chatId = 0)
1 ignored issue
show
Coding Style introduced by
eventsAction uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
    {
15
        $chatId = (int) $chatId;
16
        $result = new Server\Result();
17 View Code Duplication
        if (!$chatId || !($chat = \Chats\Chat::get($chatId))) {
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...
18
            $result->success = false;
19
            $result->content = 'Такого чата не существует';
20
            $result->send();
21
        }
22
        $where = [
23
            ['chat_id', $chatId],
24
            ['delete', 0]
25
        ];
26
        if (!empty($_GET['lastEventDate'])) {
27
            $where[] = ['date_create', $_GET['lastEventDate'], '>'];
28
        }
29
        $result->content = [
30
            'members' => [],
31
            'messages' => []
32
        ];
33
        if (Users\User::$cur->id) {
34
            $member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]);
35
            if (!$member) {
36
                $member = new \Chats\Chat\Member();
37
                $member->user_id = Users\User::$cur->id;
38
                $member->chat_id = $chatId;
39
            }
40
            $member->date_last_active = date('Y-m-d H:i:s');
41
            $member->save();
42
        }
43
        $members = $this->module->getMembers($chatId);
44 View Code Duplication
        foreach ($members as $member) {
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...
45
            $result->content['members'][$member->user_id] = [
46
                'fullUserName' => $member->user->name(),
47
                'userFirstName' => $member->user->info->first_name,
48
                'userPhoto' => $member->user->info->photo ? $member->user->info->photo->path : '/static/system/images/no-image.png'
49
            ];
50
        }
51
52
        $messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]);
53
        $messages = array_reverse($messages);
54 View Code Duplication
        foreach ($messages as $message) {
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...
55
            $msg = [
56
                'message' => $message->_params,
57
                'fullUserName' => $message->user->name(),
58
                'userFirstName' => $message->user->info->first_name,
59
                'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png'
60
            ];
61
            $result->content['messages'][] = $msg;
62
        }
63
        $result->send();
64
    }
65
66
    function deleteMsgAction($messageId)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        $result = new Server\Result();
69 View Code Duplication
        if (!\Users\User::$cur->isAdmin()) {
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...
70
            $result->success = false;
71
            $result->content = 'Вы не админ';
72
            $result->send();
73
        }
74
        $msg = Chats\Chat\Message::get((int) $messageId);
75
        if (!$msg) {
76
            $result->success = false;
77
            $result->content = 'Сообщение не найдено';
78
            $result->send();
79
        }
80
        $msg->delete = 1;
81
        $msg->save();
82
        $result->successMsg = 'Сообщение удалено';
83
        $result->send();
84
    }
85
86
    function banUserAction($messageId)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
87
    {
88
        $result = new Server\Result();
89 View Code Duplication
        if (!\Users\User::$cur->isAdmin()) {
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...
90
            $result->success = false;
91
            $result->content = 'Вы не админ';
92
            $result->send();
93
        }
94
        $msg = Chats\Chat\Message::get((int) $messageId);
95
        if (!$msg) {
96
            $result->success = false;
97
            $result->content = 'Сообщение не найдено';
98
            $result->send();
99
        }
100
        $ban = new Chats\Chat\Ban();
101
        $ban->user_id = $msg->user_id;
102
        $ban->chat_id = $msg->chat_id;
103
        $ban->chat_message_id = $msg->id;
104
        $ban->save();
105
        $msgs = Chats\Chat\Message::getList(['where' => ['user_id', $ban->user_id]]);
106
        foreach ($msgs as $msg) {
107
            $msg->delete = 1;
108
            $msg->save();
109
        }
110
        $result->content = array_flip(array_keys($msgs));
111
        $result->successMsg = 'Сообщение удалено';
112
        $result->send();
113
    }
114
115
    public function sendFormAction($chatId = 0)
1 ignored issue
show
Coding Style introduced by
sendFormAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
116
    {
117
        $chatId = (int) $chatId;
118
        $result = new Server\Result();
119 View Code Duplication
        if (!$chatId || !($chat = \Chats\Chat::get($chatId))) {
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...
120
            $result->success = false;
121
            $result->content = 'Такого чата не существует';
122
            $result->send();
123
        }
124 View Code Duplication
        if (!Users\User::$cur->id) {
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...
125
            $result->success = false;
126
            $result->content = 'Вы не авторизованы';
127
            $result->send();
128
        }
129
        if (empty($_POST['chat-message']) || !trim($_POST['chat-message'])) {
130
            $result->success = false;
131
            $result->content = 'Сообщение не может быть пустым';
132
            $result->send();
133
        }
134
        if (Chats\Chat\Ban::get(['user_id', \Users\User::$cur->id])) {
135
            $result->success = false;
136
            $result->content = 'Вы не можете писать в чат';
137
            $result->send();
138
        }
139
        $message = new \Chats\Chat\Message();
140
        $message->user_id = Users\User::$cur->id;
141
        $message->chat_id = $chatId;
142
        $message->text = htmlspecialchars(trim($_POST['chat-message']));
143
        $message->save();
144
        $result->successMsg = 'Ваше сообщение было отправлено';
145
        $result->send();
146
    }
147
148
}
149