| Conditions | 3 |
| Paths | 1 |
| Total Lines | 72 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 25 | public function subscribe(ConnectionInterface $connection, stdClass $payload): bool |
||
| 26 | { |
||
| 27 | $this->verifySignature($connection, $payload); |
||
| 28 | |||
| 29 | $this->saveConnection($connection); |
||
| 30 | |||
| 31 | $user = json_decode($payload->channel_data); |
||
| 32 | |||
| 33 | $this->channelManager |
||
| 34 | ->userJoinedPresenceChannel($connection, $user, $this->getName(), $payload) |
||
| 35 | ->then(function () use ($connection) { |
||
| 36 | $this->channelManager |
||
| 37 | ->getChannelMembers($connection->app->id, $this->getName()) |
||
|
|
|||
| 38 | ->then(function ($users) use ($connection) { |
||
| 39 | $hash = []; |
||
| 40 | |||
| 41 | foreach ($users as $socketId => $user) { |
||
| 42 | $hash[$user->user_id] = $user->user_info ?? []; |
||
| 43 | } |
||
| 44 | |||
| 45 | $connection->send(json_encode([ |
||
| 46 | 'event' => 'pusher_internal:subscription_succeeded', |
||
| 47 | 'channel' => $this->getName(), |
||
| 48 | 'data' => json_encode([ |
||
| 49 | 'presence' => [ |
||
| 50 | 'ids' => collect($users)->map(function ($user) { |
||
| 51 | return (string) $user->user_id; |
||
| 52 | })->values(), |
||
| 53 | 'hash' => $hash, |
||
| 54 | 'count' => count($users), |
||
| 55 | ], |
||
| 56 | ]), |
||
| 57 | ])); |
||
| 58 | }); |
||
| 59 | }) |
||
| 60 | ->then(function () use ($connection, $user, $payload) { |
||
| 61 | // The `pusher_internal:member_added` event is triggered when a user joins a channel. |
||
| 62 | // It's quite possible that a user can have multiple connections to the same channel |
||
| 63 | // (for example by having multiple browser tabs open) |
||
| 64 | // and in this case the events will only be triggered when the first tab is opened. |
||
| 65 | $this->channelManager |
||
| 66 | ->getMemberSockets($user->user_id, $connection->app->id, $this->getName()) |
||
| 67 | ->then(function ($sockets) use ($payload, $connection, $user) { |
||
| 68 | if (count($sockets) === 1) { |
||
| 69 | $memberAddedPayload = [ |
||
| 70 | 'event' => 'pusher_internal:member_added', |
||
| 71 | 'channel' => $this->getName(), |
||
| 72 | 'data' => $payload->channel_data, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | $this->broadcastToEveryoneExcept( |
||
| 76 | (object) $memberAddedPayload, $connection->socketId, |
||
| 77 | $connection->app->id |
||
| 78 | ); |
||
| 79 | |||
| 80 | SubscribedToChannel::dispatch( |
||
| 81 | $connection->app->id, |
||
| 82 | $connection->socketId, |
||
| 83 | $this->getName(), |
||
| 84 | $user |
||
| 85 | ); |
||
| 86 | } |
||
| 87 | |||
| 88 | DashboardLogger::log($connection->app->id, DashboardLogger::TYPE_SUBSCRIBED, [ |
||
| 89 | 'socketId' => $connection->socketId, |
||
| 90 | 'channel' => $this->getName(), |
||
| 91 | 'duplicate-connection' => count($sockets) > 1, |
||
| 92 | ]); |
||
| 93 | }); |
||
| 94 | }); |
||
| 95 | |||
| 96 | return true; |
||
| 97 | } |
||
| 157 |