@@ -49,29 +49,21 @@ public function __invoke(Request $request)
49
49
return $ channel ->getChannelName ();
50
50
})->toArray ();
51
51
52
- // We ask the replication backend to get us the member count per channel
53
- $ memberCounts = $ this ->replication ->channelMemberCounts ($ request ->appId , $ channelNames );
52
+ // We ask the replication backend to get us the member count per channel.
53
+ // We get $counts back as a key-value array of channel names and their member count.
54
+ return $ this ->replication
55
+ ->channelMemberCounts ($ request ->appId , $ channelNames )
56
+ ->then (function (array $ counts ) use ($ channels , $ attributes ) {
57
+ return [
58
+ 'channels ' => $ channels ->map (function (PresenceChannel $ channel ) use ($ counts , $ attributes ) {
59
+ $ info = new \stdClass ;
60
+ if (in_array ('user_count ' , $ attributes )) {
61
+ $ info ->user_count = $ counts [$ channel ->getChannelName ()];
62
+ }
54
63
55
- // We return a promise since the backend runs async. We get $counts back
56
- // as a key-value array of channel names and their member count.
57
- return $ memberCounts ->then (function (array $ counts ) use ($ channels , $ attributes ) {
58
- return $ this ->collectUserCounts ($ channels , $ attributes , function (PresenceChannel $ channel ) use ($ counts ) {
59
- return $ counts [$ channel ->getChannelName ()];
64
+ return $ info ;
65
+ })->toArray () ?: new \stdClass ,
66
+ ];
60
67
});
61
- });
62
- }
63
-
64
- protected function collectUserCounts (Collection $ channels , array $ attributes , callable $ transformer )
65
- {
66
- return [
67
- 'channels ' => $ channels ->map (function (PresenceChannel $ channel ) use ($ transformer , $ attributes ) {
68
- $ info = new \stdClass ;
69
- if (in_array ('user_count ' , $ attributes )) {
70
- $ info ->user_count = $ transformer ($ channel );
71
- }
72
-
73
- return $ info ;
74
- })->toArray () ?: new \stdClass ,
75
- ];
76
68
}
77
69
}
0 commit comments