Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 8308a7d

Browse files
committed
formatting
1 parent 6be62b1 commit 8308a7d

15 files changed

+575
-774
lines changed

src/API/FetchChannels.php

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,48 @@ public function __invoke(Request $request)
2828
}
2929
}
3030

31-
return $this->channelManager
32-
->getGlobalChannels($request->appId)
33-
->then(function ($channels) use ($request, $attributes) {
34-
$channels = collect($channels)->keyBy(function ($channel) {
35-
return $channel instanceof Channel
36-
? $channel->getName()
37-
: $channel;
38-
});
31+
return $this->channelManager->getGlobalChannels($request->appId)->then(function ($channels) use ($request, $attributes) {
32+
$channels = collect($channels)->keyBy(function ($channel) {
33+
return $channel instanceof Channel
34+
? $channel->getName()
35+
: $channel;
36+
});
3937

40-
if ($request->has('filter_by_prefix')) {
41-
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
42-
return Str::startsWith($channelName, $request->filter_by_prefix);
43-
});
44-
}
38+
if ($request->has('filter_by_prefix')) {
39+
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
40+
return Str::startsWith($channelName, $request->filter_by_prefix);
41+
});
42+
}
4543

46-
$channelNames = $channels->map(function ($channel) {
47-
return $channel instanceof Channel
48-
? $channel->getName()
49-
: $channel;
50-
})->toArray();
44+
$channelNames = $channels->map(function ($channel) {
45+
return $channel instanceof Channel
46+
? $channel->getName()
47+
: $channel;
48+
})->toArray();
5149

52-
return $this->channelManager
53-
->getChannelsMembersCount($request->appId, $channelNames)
54-
->then(function ($counts) use ($channels, $attributes) {
55-
$channels = $channels->map(function ($channel) use ($counts, $attributes) {
56-
$info = new stdClass;
50+
return $this->channelManager
51+
->getChannelsMembersCount($request->appId, $channelNames)
52+
->then(function ($counts) use ($channels, $attributes) {
53+
$channels = $channels->map(function ($channel) use ($counts, $attributes) {
54+
$info = new stdClass;
5755

58-
$channelName = $channel instanceof Channel
59-
? $channel->getName()
60-
: $channel;
56+
$channelName = $channel instanceof Channel
57+
? $channel->getName()
58+
: $channel;
6159

62-
if (in_array('user_count', $attributes)) {
63-
$info->user_count = $counts[$channelName];
64-
}
60+
if (in_array('user_count', $attributes)) {
61+
$info->user_count = $counts[$channelName];
62+
}
6563

66-
return $info;
67-
})->sortBy(function ($content, $name) {
68-
return $name;
69-
})->all();
64+
return $info;
65+
})->sortBy(function ($content, $name) {
66+
return $name;
67+
})->all();
7068

71-
return [
72-
'channels' => $channels ?: new stdClass,
73-
];
74-
});
75-
});
69+
return [
70+
'channels' => $channels ?: new stdClass,
71+
];
72+
});
73+
});
7674
}
7775
}

src/ChannelManagers/LocalChannelManager.php

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,21 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection): Pro
163163
return Helpers::createFulfilledPromise(false);
164164
}
165165

166-
$this->getLocalChannels($connection->app->id)
167-
->then(function ($channels) use ($connection) {
168-
collect($channels)->each->unsubscribe($connection);
169-
170-
collect($channels)
171-
->reject->hasConnections()
172-
->each(function (Channel $channel, string $channelName) use ($connection) {
173-
unset($this->channels[$connection->app->id][$channelName]);
174-
});
175-
});
176-
177-
$this->getLocalChannels($connection->app->id)
178-
->then(function ($channels) use ($connection) {
179-
if (count($channels) === 0) {
180-
unset($this->channels[$connection->app->id]);
181-
}
182-
});
166+
$this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
167+
collect($channels)->each->unsubscribe($connection);
168+
169+
collect($channels)
170+
->reject->hasConnections()
171+
->each(function (Channel $channel, string $channelName) use ($connection) {
172+
unset($this->channels[$connection->app->id][$channelName]);
173+
});
174+
});
175+
176+
$this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
177+
if (count($channels) === 0) {
178+
unset($this->channels[$connection->app->id]);
179+
}
180+
});
183181

184182
return Helpers::createFulfilledPromise(true);
185183
}
@@ -252,18 +250,17 @@ public function unsubscribeFromApp($appId): PromiseInterface
252250
*/
253251
public function getLocalConnectionsCount($appId, string $channelName = null): PromiseInterface
254252
{
255-
return $this->getLocalChannels($appId)
256-
->then(function ($channels) use ($channelName) {
257-
return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
258-
return $collection->filter(function (Channel $channel) use ($channelName) {
259-
return $channel->getName() === $channelName;
260-
});
261-
})
262-
->flatMap(function (Channel $channel) {
263-
return collect($channel->getConnections())->pluck('socketId');
264-
})
265-
->unique()->count();
266-
});
253+
return $this->getLocalChannels($appId)->then(function ($channels) use ($channelName) {
254+
return collect($channels)->when(! is_null($channelName), function ($collection) use ($channelName) {
255+
return $collection->filter(function (Channel $channel) use ($channelName) {
256+
return $channel->getName() === $channelName;
257+
});
258+
})
259+
->flatMap(function (Channel $channel) {
260+
return collect($channel->getConnections())->pluck('socketId');
261+
})
262+
->unique()->count();
263+
});
267264
}
268265

269266
/**
@@ -455,16 +452,15 @@ public function removeObsoleteConnections(): PromiseInterface
455452
*/
456453
public function updateConnectionInChannels($connection): PromiseInterface
457454
{
458-
return $this->getLocalChannels($connection->app->id)
459-
->then(function ($channels) use ($connection) {
460-
foreach ($channels as $channel) {
461-
if ($channel->hasConnection($connection)) {
462-
$channel->saveConnection($connection);
463-
}
455+
return $this->getLocalChannels($connection->app->id)->then(function ($channels) use ($connection) {
456+
foreach ($channels as $channel) {
457+
if ($channel->hasConnection($connection)) {
458+
$channel->saveConnection($connection);
464459
}
460+
}
465461

466-
return true;
467-
});
462+
return true;
463+
});
468464
}
469465

470466
/**

src/ChannelManagers/RedisChannelManager.php

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,13 @@ public function getGlobalChannels($appId): PromiseInterface
137137
*/
138138
public function unsubscribeFromAllChannels(ConnectionInterface $connection): PromiseInterface
139139
{
140-
return $this->getGlobalChannels($connection->app->id)
141-
->then(function ($channels) use ($connection) {
142-
foreach ($channels as $channel) {
143-
$this->unsubscribeFromChannel($connection, $channel, new stdClass);
144-
}
145-
})
146-
->then(function () use ($connection) {
147-
return parent::unsubscribeFromAllChannels($connection);
148-
});
140+
return $this->getGlobalChannels($connection->app->id)->then(function ($channels) use ($connection) {
141+
foreach ($channels as $channel) {
142+
$this->unsubscribeFromChannel($connection, $channel, new stdClass);
143+
}
144+
})->then(function () use ($connection) {
145+
return parent::unsubscribeFromAllChannels($connection);
146+
});
149147
}
150148

151149
/**
@@ -158,19 +156,15 @@ public function unsubscribeFromAllChannels(ConnectionInterface $connection): Pro
158156
*/
159157
public function subscribeToChannel(ConnectionInterface $connection, string $channelName, stdClass $payload): PromiseInterface
160158
{
161-
return $this->subscribeToTopic($connection->app->id, $channelName)
162-
->then(function () use ($connection) {
163-
return $this->addConnectionToSet($connection, Carbon::now());
164-
})
165-
->then(function () use ($connection, $channelName) {
166-
return $this->addChannelToSet($connection->app->id, $channelName);
167-
})
168-
->then(function () use ($connection, $channelName) {
169-
return $this->incrementSubscriptionsCount($connection->app->id, $channelName, 1);
170-
})
171-
->then(function () use ($connection, $channelName, $payload) {
172-
return parent::subscribeToChannel($connection, $channelName, $payload);
173-
});
159+
return $this->subscribeToTopic($connection->app->id, $channelName)->then(function () use ($connection) {
160+
return $this->addConnectionToSet($connection, Carbon::now());
161+
})->then(function () use ($connection, $channelName) {
162+
return $this->addChannelToSet($connection->app->id, $channelName);
163+
})->then(function () use ($connection, $channelName) {
164+
return $this->incrementSubscriptionsCount($connection->app->id, $channelName, 1);
165+
})->then(function () use ($connection, $channelName, $payload) {
166+
return parent::subscribeToChannel($connection, $channelName, $payload);
167+
});
174168
}
175169

176170
/**
@@ -199,14 +193,11 @@ public function unsubscribeFromChannel(ConnectionInterface $connection, string $
199193
$this->unsubscribeFromTopic($connection->app->id, $channelName);
200194
}
201195
});
202-
})
203-
->then(function () use ($connection, $channelName) {
196+
})->then(function () use ($connection, $channelName) {
204197
return $this->removeChannelFromSet($connection->app->id, $channelName);
205-
})
206-
->then(function () use ($connection) {
198+
})->then(function () use ($connection) {
207199
return $this->removeConnectionFromSet($connection);
208-
})
209-
->then(function () use ($connection, $channelName, $payload) {
200+
})->then(function () use ($connection, $channelName, $payload) {
210201
return parent::unsubscribeFromChannel($connection, $channelName, $payload);
211202
});
212203
}
@@ -220,10 +211,9 @@ public function unsubscribeFromChannel(ConnectionInterface $connection, string $
220211
*/
221212
public function subscribeToApp($appId): PromiseInterface
222213
{
223-
return $this->subscribeToTopic($appId)
224-
->then(function () use ($appId) {
225-
return $this->incrementSubscriptionsCount($appId);
226-
});
214+
return $this->subscribeToTopic($appId)->then(function () use ($appId) {
215+
return $this->incrementSubscriptionsCount($appId);
216+
});
227217
}
228218

229219
/**
@@ -235,10 +225,9 @@ public function subscribeToApp($appId): PromiseInterface
235225
*/
236226
public function unsubscribeFromApp($appId): PromiseInterface
237227
{
238-
return $this->unsubscribeFromTopic($appId)
239-
->then(function () use ($appId) {
240-
return $this->decrementSubscriptionsCount($appId);
241-
});
228+
return $this->unsubscribeFromTopic($appId)->then(function () use ($appId) {
229+
return $this->decrementSubscriptionsCount($appId);
230+
});
242231
}
243232

244233
/**
@@ -308,8 +297,7 @@ public function userJoinedPresenceChannel(ConnectionInterface $connection, stdCl
308297
return $this->storeUserData($connection->app->id, $channel, $connection->socketId, json_encode($user))
309298
->then(function () use ($connection, $channel, $user) {
310299
return $this->addUserSocket($connection->app->id, $channel, $user, $connection->socketId);
311-
})
312-
->then(function () use ($connection, $user, $channel, $payload) {
300+
})->then(function () use ($connection, $user, $channel, $payload) {
313301
return parent::userJoinedPresenceChannel($connection, $user, $channel, $payload);
314302
});
315303
}
@@ -328,8 +316,7 @@ public function userLeftPresenceChannel(ConnectionInterface $connection, stdClas
328316
return $this->removeUserData($connection->app->id, $channel, $connection->socketId)
329317
->then(function () use ($connection, $channel, $user) {
330318
return $this->removeUserSocket($connection->app->id, $channel, $user, $connection->socketId);
331-
})
332-
->then(function () use ($connection, $user, $channel) {
319+
})->then(function () use ($connection, $user, $channel) {
333320
return parent::userLeftPresenceChannel($connection, $user, $channel);
334321
});
335322
}
@@ -383,10 +370,9 @@ public function getChannelsMembersCount($appId, array $channelNames): PromiseInt
383370
);
384371
}
385372

386-
return $this->publishClient->exec()
387-
->then(function ($data) use ($channelNames) {
388-
return array_combine($channelNames, $data);
389-
});
373+
return $this->publishClient->exec()->then(function ($data) use ($channelNames) {
374+
return array_combine($channelNames, $data);
375+
});
390376
}
391377

392378
/**
@@ -413,10 +399,9 @@ public function getMemberSockets($userId, $appId, $channelName): PromiseInterfac
413399
public function connectionPonged(ConnectionInterface $connection): PromiseInterface
414400
{
415401
// This will update the score with the current timestamp.
416-
return $this->addConnectionToSet($connection, Carbon::now())
417-
->then(function () use ($connection) {
418-
return parent::connectionPonged($connection);
419-
});
402+
return $this->addConnectionToSet($connection, Carbon::now())->then(function () use ($connection) {
403+
return parent::connectionPonged($connection);
404+
});
420405
}
421406

422407
/**

0 commit comments

Comments
 (0)