Passed
Push — master ( 45d699...d5e463 )
by Julito
12:21
created

ChatController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 73
c 1
b 0
f 0
dl 0
loc 124
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
C ajaxAction() 0 94 12
A indexAction() 0 16 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Chamilo\CoreBundle\Repository\ResourceNodeRepository;
8
use Chamilo\CourseBundle\Controller\CourseControllerInterface;
9
use Chamilo\CourseBundle\Controller\CourseControllerTrait;
10
use Chamilo\CourseBundle\Repository\CChatConversationRepository;
11
use Event;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * Class ChatController.
19
 */
20
class ChatController extends AbstractResourceController implements CourseControllerInterface
21
{
22
    use CourseControllerTrait;
23
24
    /**
25
     * @Route("/resources/chat/", name="chat_home", options={"expose"=true})
26
     */
27
    public function indexAction(Request $request): Response
28
    {
29
        Event::event_access_tool(TOOL_CHAT);
0 ignored issues
show
Bug introduced by
The method event_access_tool() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        Event::/** @scrutinizer ignore-call */ 
30
               event_access_tool(TOOL_CHAT);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
31
        $logInfo = [
32
            'tool' => TOOL_CHAT,
33
            'action' => 'start',
34
            'action_details' => 'start-chat',
35
        ];
36
        Event::registerLog($logInfo);
0 ignored issues
show
Bug introduced by
The method registerLog() does not exist on Event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        Event::/** @scrutinizer ignore-call */ 
37
               registerLog($logInfo);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
        return $this->render(
39
            '@ChamiloTheme/Chat/chat.html.twig',
40
            [
41
                'restrict_to_coach' => api_get_configuration_value('course_chat_restrict_to_coach'),
42
                'user' => api_get_user_info(),
43
            ]
44
        );
45
    }
46
47
    /**
48
     * @Route("/resources/chat/conversations/", name="chat_ajax", options={"expose"=true})
49
     */
50
    public function ajaxAction(Request $request, ResourceNodeRepository $repo): Response
51
    {
52
        if (!api_protect_course_script(false)) {
53
            exit;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Symfony\Component\HttpFoundation\Response. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
54
        }
55
56
        /** @var CChatConversationRepository $resourceRepo */
57
        $resourceRepo = $this->getRepository('chat', 'conversations');
58
59
        $courseId = api_get_course_int_id();
60
        $userId = api_get_user_id();
61
        $sessionId = api_get_session_id();
62
        $groupId = api_get_group_id();
63
        $json = ['status' => false];
64
        $parentResourceNode = $this->getParentResourceNode($request);
65
66
        $courseChatUtils = new \CourseChatUtils(
67
            $courseId,
68
            $userId,
69
            $sessionId,
70
            $groupId,
71
            $parentResourceNode,
72
            $resourceRepo
73
        );
74
75
        $action = $request->get('action');
76
77
        switch ($action) {
78
            case 'chat_logout':
79
                $logInfo = [
80
                    'tool' => TOOL_CHAT,
81
                    'action' => 'exit',
82
                    'action_details' => 'exit-chat',
83
                ];
84
                Event::registerLog($logInfo);
85
86
                break;
87
            case 'track':
88
                $courseChatUtils->keepUserAsConnected();
89
                $courseChatUtils->disconnectInactiveUsers();
90
91
                $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
92
                //$filePath = $courseChatUtils->getFileName(true, $friend);
93
                //$newFileSize = file_exists($filePath) ? filesize($filePath) : 0;
94
                //$oldFileSize = isset($_GET['size']) ? (int) $_GET['size'] : -1;
95
                $newUsersOnline = $courseChatUtils->countUsersOnline();
96
                $oldUsersOnline = isset($_GET['users_online']) ? (int) $_GET['users_online'] : 0;
97
98
                $json = [
99
                    'status' => true,
100
                    'data' => [
101
                        //'oldFileSize' => file_exists($filePath) ? filesize($filePath) : 0,
102
                        'oldFileSize' => false,
103
                        'history' => $courseChatUtils->readMessages(false, $friend),
104
                        'usersOnline' => $newUsersOnline,
105
                        'userList' => $newUsersOnline != $oldUsersOnline ? $courseChatUtils->listUsersOnline() : null,
106
                        'currentFriend' => $friend,
107
                    ],
108
                ];
109
110
                break;
111
            case 'preview':
112
                $json = [
113
                    'status' => true,
114
                    'data' => [
115
                        'message' => $courseChatUtils->prepareMessage($_REQUEST['message']),
116
                    ],
117
                ];
118
119
                break;
120
            case 'reset':
121
                $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
122
123
                $json = [
124
                    'status' => true,
125
                    'data' => $courseChatUtils->readMessages(true, $friend),
126
                ];
127
128
                break;
129
            case 'write':
130
                $friend = isset($_REQUEST['friend']) ? (int) $_REQUEST['friend'] : 0;
131
                $status = $courseChatUtils->saveMessage($_REQUEST['message'], $friend);
132
133
                $json = [
134
                    'status' => $status,
135
                    'data' => [
136
                        'writed' => $status,
137
                    ],
138
                ];
139
140
                break;
141
        }
142
143
        return new JsonResponse($json);
144
    }
145
}
146