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

UpdateUserInfoController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 25
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 22 4
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