Skip to content

Commit

Permalink
Update ArrayChannelManager.php
Browse files Browse the repository at this point in the history
  • Loading branch information
RajaAsjad authored Dec 28, 2024
1 parent a058259 commit ccb6b97
Showing 1 changed file with 25 additions and 37 deletions.
62 changes: 25 additions & 37 deletions src/Protocols/Pusher/Managers/ArrayChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ class ArrayChannelManager implements ChannelManagerInterface
/**
* The underlying array of applications and their channels.
*
* @var array<string, array<string, array<string, \Laravel\Reverb\Protocols\Pusher\Channels\Channel>>>
* @var array<string, array<string, Channel>>
*/
protected $applications = [];
protected array $applications = [];

/**
* The application instance.
*
* @var \Laravel\Reverb\Application
* @var Application|null
*/
protected $application;
protected ?Application $application = null;

/**
* Get the application instance.
Expand All @@ -40,40 +40,44 @@ public function app(): ?Application
}

/**
* Get all the channels.
* Get all channels for the current application.
*
* @return array<string, \Laravel\Reverb\Protocols\Pusher\Channels\Channel>
* @return array<string, Channel>
*/
public function all(): array
{
return $this->channels();
}

/**
* Determine whether the given channel exists.
* Check if the given channel exists.
*/
public function exists(string $channel): bool
{
return isset($this->applications[$this->application->id()][$channel]);
}

/**
* Find the given channel
* Find a specific channel.
*/
public function find(string $channel): ?Channel
{
return $this->channels($channel);
}

/**
* Find the given channel or create it if it doesn't exist.
* Find or create a channel by name.
*/
public function findOrCreate(string $channelName): Channel
{
if ($channel = $this->find($channelName)) {
return $channel;
}
return $this->find($channelName) ?? $this->createChannel($channelName);
}

/**
* Create a new channel and dispatch an event.
*/
protected function createChannel(string $channelName): Channel
{
$channel = ChannelBroker::create($channelName);

$this->applications[$this->application->id()][$channel->name()] = $channel;
Expand All @@ -84,21 +88,19 @@ public function findOrCreate(string $channelName): Channel
}

/**
* Get all of the connections for the given channels.
* Get all connections for a specific channel or all channels.
*
* @return array<string, \Laravel\Reverb\Protocols\Pusher\Channels\ChannelConnection>
*/
public function connections(?string $channel = null): array
{
$channels = Arr::wrap($this->channels($channel));

return array_reduce($channels, function ($carry, $channel) {
return $carry + $channel->connections();
}, []);
return array_reduce($channels, fn ($carry, $channel) => $carry + $channel->connections(), []);
}

/**
* Unsubscribe from all channels.
* Unsubscribe a connection from all channels.
*/
public function unsubscribeFromAll(Connection $connection): void
{
Expand All @@ -108,7 +110,7 @@ public function unsubscribeFromAll(Connection $connection): void
}

/**
* Remove the given channel.
* Remove a specific channel and dispatch an event.
*/
public function remove(Channel $channel): void
{
Expand All @@ -118,38 +120,24 @@ public function remove(Channel $channel): void
}

/**
* Get the given channel.
*/
public function channel(string $channel): ?Channel
{
return $this->channels($channel);
}

/**
* Get the channels for the application.
* Get a specific channel or all channels for the application.
*
* @return \Laravel\Reverb\Protocols\Pusher\Channels\Channel|array<string, \Laravel\Reverb\Protocols\Pusher\Channels\Channel>|null
* @return Channel|array<string, Channel>|null
*/
public function channels(?string $channel = null): Channel|array|null
{
$channels = $this->applications[$this->application->id()] ?? [];

if (isset($channel)) {
return $channels[$channel] ?? null;
}

return $channels;
return $channel ? ($channels[$channel] ?? null) : $channels;
}

/**
* Flush the channel manager repository.
* Flush all channels for all applications.
*/
public function flush(): void
{
app(ApplicationProvider::class)
->all()
->each(function (Application $application) {
$this->applications[$application->id()] = [];
});
->each(fn (Application $application) => $this->applications[$application->id()] = []);
}
}

0 comments on commit ccb6b97

Please sign in to comment.