VideoChat::getChatRoomByUsers()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 2
dl 0
loc 19
rs 9.9
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
/**
5
 * VideoChat class.
6
 *
7
 * This class provides methods for video chat management.
8
 *
9
 * @author Angel Fernando Quiroz Campos <[email protected]>
10
 */
11
class VideoChat
12
{
13
    /**
14
     * Get the video chat info by its users.
15
     *
16
     * @param int $user1 User id
17
     * @param int $user2 Other user id
18
     *
19
     * @return array The video chat info. Otherwise return false
20
     */
21
    public static function getChatRoomByUsers($user1, $user2)
22
    {
23
        $user1 = (int) $user1;
24
        $user2 = (int) $user2;
25
26
        if (empty($user1) || empty($user2)) {
27
            return false;
28
        }
29
30
        return Database::select(
31
            '*',
32
            Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
33
            [
34
                'where' => [
35
                    '(from_user = ? AND to_user = ?)' => [$user1, $user2],
36
                    'OR (from_user = ? AND to_user = ?)' => [$user2, $user1],
37
                ],
38
            ],
39
            'first'
40
        );
41
    }
42
43
    /**
44
     * Create a video chat.
45
     *
46
     * @param int $fromUser The sender user
47
     * @param int $toUser   The receiver user
48
     *
49
     * @return int The created video chat id. Otherwise return false
50
     */
51
    public static function createRoom($fromUser, $toUser)
52
    {
53
        $fromUserInfo = api_get_user_info($fromUser);
54
        $toUserInfo = api_get_user_info($toUser);
55
56
        $chatName = vsprintf(
57
            get_lang('VideoChatBetweenUserXAndUserY'),
58
            [$fromUserInfo['firstname'], $toUserInfo['firstname']]
59
        );
60
61
        return Database::insert(
0 ignored issues
show
Bug Best Practice introduced by
The expression return Database::insert(...pi_get_utc_datetime())) could also return false which is incompatible with the documented return type integer. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
62
            Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
63
            [
64
                'from_user' => $fromUser,
65
                'to_user' => $toUser,
66
                'room_name' => $chatName,
67
                'datetime' => api_get_utc_datetime(),
68
            ]
69
        );
70
    }
71
72
    /**
73
     * Check if the video chat exists by its room name.
74
     *
75
     * @param string $name The video chat name
76
     *
77
     * @return bool
78
     */
79
    public static function nameExists($name)
80
    {
81
        $resultData = Database::select(
82
            'COUNT(1) AS count',
83
            Database::get_main_table(TABLE_MAIN_CHAT_VIDEO),
84
            [
85
                'where' => ['room_name = ?' => $name],
86
            ],
87
            'first'
88
        );
89
90
        if ($resultData !== false) {
91
            return $resultData['count'] > 0;
92
        }
93
94
        return false;
95
    }
96
}
97