Authenticatable::isUserIdOnline()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SwooleTW\Http\Websocket;
4
5
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
6
use InvalidArgumentException;
7
8
/**
9
 * Trait Authenticatable
10
 *
11
 * @property-read \SwooleTW\Http\Websocket\Rooms\RoomContract $room
12
 */
13
trait Authenticatable
14
{
15
    protected $userId;
16
17
    /**
18
     * Login using current user.
19
     *
20
     * @param \Illuminate\Contracts\Auth\Authenticatable $user
21
     *
22
     * @return mixed
23
     */
24
    public function loginUsing(AuthenticatableContract $user)
25
    {
26
        return $this->loginUsingId($user->getAuthIdentifier());
27
    }
28
29
    /**
30
     * Login using current userId.
31
     *
32
     * @param $userId
33
     *
34
     * @return mixed
35
     */
36
    public function loginUsingId($userId)
37
    {
38
        return $this->join(static::USER_PREFIX . $userId);
0 ignored issues
show
Bug introduced by
It seems like join() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

38
        return $this->/** @scrutinizer ignore-call */ join(static::USER_PREFIX . $userId);
Loading history...
Bug introduced by
The constant SwooleTW\Http\Websocket\...nticatable::USER_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
    }
40
41
    /**
42
     * Logout with current sender's fd.
43
     *
44
     * @return mixed
45
     */
46
    public function logout()
47
    {
48
        if (is_null($userId = $this->getUserId())) {
49
            return null;
50
        }
51
52
        return $this->leave(static::USER_PREFIX . $userId);
0 ignored issues
show
Bug introduced by
The constant SwooleTW\Http\Websocket\...nticatable::USER_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
It seems like leave() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

52
        return $this->/** @scrutinizer ignore-call */ leave(static::USER_PREFIX . $userId);
Loading history...
53
    }
54
55
    /**
56
     * Set multiple recepients' fds by users.
57
     *
58
     * @param $users
59
     *
60
     * @return \SwooleTW\Http\Websocket\Authenticatable
61
     */
62
    public function toUser($users)
63
    {
64
        $users = is_object($users) ? func_get_args() : $users;
65
66
        $userIds = array_map(function (AuthenticatableContract $user) {
67
            $this->checkUser($user);
68
69
            return $user->getAuthIdentifier();
70
        }, $users);
71
72
        return $this->toUserId($userIds);
73
    }
74
75
    /**
76
     * Set multiple recepients' fds by userIds.
77
     *
78
     * @param $userIds
79
     *
80
     * @return \SwooleTW\Http\Websocket\Authenticatable
81
     */
82
    public function toUserId($userIds)
83
    {
84
        $userIds = is_string($userIds) || is_integer($userIds) ? func_get_args() : $userIds;
85
86
        foreach ($userIds as $userId) {
87
            $fds = $this->room->getClients(static::USER_PREFIX . $userId);
0 ignored issues
show
Bug introduced by
The constant SwooleTW\Http\Websocket\...nticatable::USER_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
            $this->to($fds);
0 ignored issues
show
Bug introduced by
It seems like to() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

88
            $this->/** @scrutinizer ignore-call */ 
89
                   to($fds);
Loading history...
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * Get current auth user id by sender's fd.
96
     */
97
    public function getUserId()
98
    {
99
        if (! is_null($this->userId)) {
100
            return $this->userId;
101
        }
102
103
        $rooms = $this->room->getRooms($this->getSender());
0 ignored issues
show
Bug introduced by
It seems like getSender() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

103
        $rooms = $this->room->getRooms($this->/** @scrutinizer ignore-call */ getSender());
Loading history...
104
105
        foreach ($rooms as $room) {
106
            if (count($explode = explode(static::USER_PREFIX, $room)) === 2) {
0 ignored issues
show
Bug introduced by
The constant SwooleTW\Http\Websocket\...nticatable::USER_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
107
                $this->userId = $explode[1];
108
            }
109
        }
110
111
        return $this->userId;
112
    }
113
114
    /**
115
     * Check if a user is online by given userId.
116
     *
117
     * @param $userId
118
     *
119
     * @return bool
120
     */
121
    public function isUserIdOnline($userId)
122
    {
123
        return ! empty($this->room->getClients(static::USER_PREFIX . $userId));
0 ignored issues
show
Bug introduced by
The constant SwooleTW\Http\Websocket\...nticatable::USER_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
124
    }
125
126
    /**
127
     * Check if user object implements AuthenticatableContract.
128
     *
129
     * @param $user
130
     */
131
    protected function checkUser($user)
132
    {
133
        if (! $user instanceOf AuthenticatableContract) {
134
            throw new InvalidArgumentException('user object must implement ' . AuthenticatableContract::class);
135
        }
136
    }
137
}
138