Passed
Push — master ( edbfbd...89a048 )
by Alexey
05:17
created

ChatsController::banUserAction()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 27
Code Lines 23

Duplication

Lines 5
Ratio 18.52 %

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 8
nop 1
dl 5
loc 27
rs 8.5806
c 0
b 0
f 0
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
        $chatId = (int) $chatId;
15
        $result = new Server\Result();
16 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...
17
            $result->success = false;
18
            $result->content = 'Такого чата не существует';
19
            $result->send();
20
        }
21
        $where = [
22
            ['chat_id', $chatId],
23
            ['delete', 0]
24
        ];
25
        if (!empty($_GET['lastEventDate'])) {
26
            $where[] = ['date_create', $_GET['lastEventDate'], '>'];
27
        }
28
        $result->content = [
29
            'members' => [],
30
            'messages' => []
31
        ];
32
        if (Users\User::$cur->id) {
33
            $member = \Chats\Chat\Member::get([['chat_id', $chatId], ['user_id', Users\User::$cur->id]]);
34
            if (!$member) {
35
                $member = new \Chats\Chat\Member();
36
                $member->user_id = Users\User::$cur->id;
37
                $member->chat_id = $chatId;
38
            }
39
            $member->date_last_active = date('Y-m-d H:i:s');
40
            $member->save();
41
        }
42
43
        $messages = \Chats\Chat\Message::getList(['where' => $where, 'limit' => 20, 'order' => ['date_create', 'DESC']]);
44
        if ($messages) {
45
            $messages = array_reverse($messages);
46
            foreach ($messages as $message) {
47
                $msg = [
48
                    'message' => $message->_params,
49
                    'fullUserName' => $message->user->name(),
50
                    'userFirstName' => $message->user->info->first_name,
51
                    'userPhoto' => $message->user->info->photo ? $message->user->info->photo->path : '/static/system/images/no-image.png'
52
                ];
53
                $result->content['messages'][] = $msg;
54
            }
55
        }
56
        $result->send();
57
    }
58
59
    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...
60
        $result = new Server\Result();
61 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...
62
            $result->success = false;
63
            $result->content = 'Вы не админ';
64
            $result->send();
65
        }
66
        $msg = Chats\Chat\Message::get((int) $messageId);
67
        if (!$msg) {
68
            $result->success = false;
69
            $result->content = 'Сообщение не найдено';
70
            $result->send();
71
        }
72
        $msg->delete = 1;
73
        $msg->save();
74
        $result->successMsg = 'Сообщение удалено';
75
        $result->send();
76
    }
77
78
    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...
79
        $result = new Server\Result();
80 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...
81
            $result->success = false;
82
            $result->content = 'Вы не админ';
83
            $result->send();
84
        }
85
        $msg = Chats\Chat\Message::get((int) $messageId);
86
        if (!$msg) {
87
            $result->success = false;
88
            $result->content = 'Сообщение не найдено';
89
            $result->send();
90
        }
91
        $ban = new Chats\Chat\Ban();
92
        $ban->user_id = $msg->user_id;
93
        $ban->chat_id = $msg->chat_id;
94
        $ban->chat_message_id = $msg->id;
95
        $ban->save();
96
        $msgs = Chats\Chat\Message::getList(['where' => ['user_id', $ban->user_id]]);
97
        foreach ($msgs as $msg) {
98
            $msg->delete = 1;
99
            $msg->save();
100
        }
101
        $result->content = array_flip(array_keys($msgs));
102
        $result->successMsg = 'Сообщение удалено';
103
        $result->send();
104
    }
105
106
    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...
107
        $chatId = (int) $chatId;
108
        $result = new Server\Result();
109 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...
110
            $result->success = false;
111
            $result->content = 'Такого чата не существует';
112
            $result->send();
113
        }
114 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...
115
            $result->success = false;
116
            $result->content = 'Вы не авторизованы';
117
            $result->send();
118
        }
119
        if (empty($_POST['chat-message']) || !trim($_POST['chat-message'])) {
120
            $result->success = false;
121
            $result->content = 'Сообщение не может быть пустым';
122
            $result->send();
123
        }
124
        if (Chats\Chat\Ban::get(['user_id', \Users\User::$cur->id])) {
125
            $result->success = false;
126
            $result->content = 'Вы не можете писать в чат';
127
            $result->send();
128
        }
129
        $message = new \Chats\Chat\Message();
130
        $message->user_id = Users\User::$cur->id;
131
        $message->chat_id = $chatId;
132
        $message->text = htmlspecialchars(trim($_POST['chat-message']));
133
        $message->save();
134
        $result->successMsg = 'Ваше сообщение было отправлено';
135
        $result->send();
136
    }
137
138
}
139