Completed
Pull Request — master (#401)
by
unknown
01:21
created

UpdateUserInfoController::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
4
5
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Collection;
8
use Symfony\Component\HttpKernel\Exception\HttpException;
9
10
class UpdateUserInfoController extends Controller
11
{
12
    public function __invoke(Request $request)
13
    {
14
        $channel = $this->channelManager->find($request->appId, $request->channelName);
15
16
        if (is_null($channel)) {
17
            throw new HttpException(404, 'Unknown channel "'.$request->channelName.'"');
18
        }
19
20
        if (! $channel instanceof PresenceChannel) {
21
            throw new HttpException(400, 'Invalid presence channel "'.$request->channelName.'"');
22
        }
23
24
        $user = Collection::make($channel->getUsers())->filter(function($user) use ($request) {
25
            return $user->user_id === (int) $request->userId;
26
        })->first();
27
28
        if (is_null($user)) {
29
            throw new HttpException(404, 'Unknown user "'.$request->userId.'"');
30
        }
31
32
        $channel->updateUserInfo($user->user_id, (object) $request->info);
33
    }
34
}
35