From 9b4e2439a406a8492f01c3f8376f2874a1c7996d Mon Sep 17 00:00:00 2001 From: Amir Reza Mehbakhsh Date: Sat, 13 Apr 2024 18:28:42 +0200 Subject: [PATCH 1/5] Add exists() method to ChannelManager --- src/Protocols/Pusher/Contracts/ChannelManager.php | 5 +++++ src/Protocols/Pusher/Managers/ArrayChannelManager.php | 5 +++++ .../Unit/Protocols/Pusher/Managers/ChannelManagerTest.php | 7 +++++++ 3 files changed, 17 insertions(+) diff --git a/src/Protocols/Pusher/Contracts/ChannelManager.php b/src/Protocols/Pusher/Contracts/ChannelManager.php index 0b76e9cb..bd2dafbd 100644 --- a/src/Protocols/Pusher/Contracts/ChannelManager.php +++ b/src/Protocols/Pusher/Contracts/ChannelManager.php @@ -30,6 +30,11 @@ public function all(): array; */ public function find(string $channel): ?Channel; + /** + * Check whether the channel exists or not. + */ + public function exists(string $channel): bool; + /** * Find the given channel or create it if it doesn't exist. */ diff --git a/src/Protocols/Pusher/Managers/ArrayChannelManager.php b/src/Protocols/Pusher/Managers/ArrayChannelManager.php index 0965d337..ffad4ec8 100644 --- a/src/Protocols/Pusher/Managers/ArrayChannelManager.php +++ b/src/Protocols/Pusher/Managers/ArrayChannelManager.php @@ -55,6 +55,11 @@ public function find(string $channel): ?Channel return $this->channels($channel); } + public function exists(string $channel): bool + { + return isset($this->applications[$this->application->id()][$channel]); + } + /** * Find the given channel or create it if it doesn't exist. */ diff --git a/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php b/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php index 787f99a9..dc7242bd 100644 --- a/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php +++ b/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php @@ -38,6 +38,13 @@ expect($this->channelManager->all())->toHaveCount(4); }); +it('can determine if channel exists or not', function () { + $this->channelManager->findOrCreate('test-channel-1'); + + expect($this->channelManager->exists('test-channel-1'))->toBeTrue(); + expect($this->channelManager->exists('test-channel-2'))->toBeFalse(); +}); + it('can get all connections subscribed to a channel', function () { $connections = collect(factory(5)) ->each(fn ($connection) => $this->channel->subscribe($connection->connection())); From 53e91c51db099cca5153c68e4c76a920ad9744e0 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Mon, 15 Apr 2024 09:15:01 +0100 Subject: [PATCH 2/5] formatting --- src/Protocols/Pusher/Contracts/ChannelManager.php | 2 +- src/Protocols/Pusher/Managers/ArrayChannelManager.php | 3 +++ tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Protocols/Pusher/Contracts/ChannelManager.php b/src/Protocols/Pusher/Contracts/ChannelManager.php index bd2dafbd..192539f0 100644 --- a/src/Protocols/Pusher/Contracts/ChannelManager.php +++ b/src/Protocols/Pusher/Contracts/ChannelManager.php @@ -31,7 +31,7 @@ public function all(): array; public function find(string $channel): ?Channel; /** - * Check whether the channel exists or not. + * Determine whether the channel exists. */ public function exists(string $channel): bool; diff --git a/src/Protocols/Pusher/Managers/ArrayChannelManager.php b/src/Protocols/Pusher/Managers/ArrayChannelManager.php index ffad4ec8..e538d9df 100644 --- a/src/Protocols/Pusher/Managers/ArrayChannelManager.php +++ b/src/Protocols/Pusher/Managers/ArrayChannelManager.php @@ -55,6 +55,9 @@ public function find(string $channel): ?Channel return $this->channels($channel); } + /** + * Determine whether the channel exists. + */ public function exists(string $channel): bool { return isset($this->applications[$this->application->id()][$channel]); diff --git a/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php b/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php index dc7242bd..638ae60c 100644 --- a/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php +++ b/tests/Unit/Protocols/Pusher/Managers/ChannelManagerTest.php @@ -38,7 +38,7 @@ expect($this->channelManager->all())->toHaveCount(4); }); -it('can determine if channel exists or not', function () { +it('can determine whether a channel exists', function () { $this->channelManager->findOrCreate('test-channel-1'); expect($this->channelManager->exists('test-channel-1'))->toBeTrue(); From 298087020ec842a1c1724d3fcbb061eeed505230 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Mon, 15 Apr 2024 09:16:52 +0100 Subject: [PATCH 3/5] formatting --- src/Protocols/Pusher/Contracts/ChannelManager.php | 2 +- src/Protocols/Pusher/Managers/ArrayChannelManager.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Protocols/Pusher/Contracts/ChannelManager.php b/src/Protocols/Pusher/Contracts/ChannelManager.php index 192539f0..c4c7b2ed 100644 --- a/src/Protocols/Pusher/Contracts/ChannelManager.php +++ b/src/Protocols/Pusher/Contracts/ChannelManager.php @@ -31,7 +31,7 @@ public function all(): array; public function find(string $channel): ?Channel; /** - * Determine whether the channel exists. + * Determine whether the given channel exists. */ public function exists(string $channel): bool; diff --git a/src/Protocols/Pusher/Managers/ArrayChannelManager.php b/src/Protocols/Pusher/Managers/ArrayChannelManager.php index e538d9df..18d4be28 100644 --- a/src/Protocols/Pusher/Managers/ArrayChannelManager.php +++ b/src/Protocols/Pusher/Managers/ArrayChannelManager.php @@ -56,7 +56,7 @@ public function find(string $channel): ?Channel } /** - * Determine whether the channel exists. + * Determine whether the given channel exists. */ public function exists(string $channel): bool { From 65a3c198e6b8eaafcd03785df6cd7c64be781775 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 15 Apr 2024 10:15:49 -0500 Subject: [PATCH 4/5] Update ChannelManager.php --- src/Protocols/Pusher/Contracts/ChannelManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Protocols/Pusher/Contracts/ChannelManager.php b/src/Protocols/Pusher/Contracts/ChannelManager.php index c4c7b2ed..4acb2f9e 100644 --- a/src/Protocols/Pusher/Contracts/ChannelManager.php +++ b/src/Protocols/Pusher/Contracts/ChannelManager.php @@ -26,14 +26,14 @@ public function for(Application $application): ChannelManager; public function all(): array; /** - * Find the given channel. + * Determine whether the given channel exists. */ - public function find(string $channel): ?Channel; + public function exists(string $channel): bool; /** - * Determine whether the given channel exists. + * Find the given channel. */ - public function exists(string $channel): bool; + public function find(string $channel): ?Channel; /** * Find the given channel or create it if it doesn't exist. From db74a5c647e0446e6b69ec6e938cad96650a4970 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 15 Apr 2024 10:16:08 -0500 Subject: [PATCH 5/5] Update ArrayChannelManager.php --- .../Pusher/Managers/ArrayChannelManager.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Protocols/Pusher/Managers/ArrayChannelManager.php b/src/Protocols/Pusher/Managers/ArrayChannelManager.php index 18d4be28..a8cf694c 100644 --- a/src/Protocols/Pusher/Managers/ArrayChannelManager.php +++ b/src/Protocols/Pusher/Managers/ArrayChannelManager.php @@ -48,19 +48,19 @@ public function all(): array } /** - * Find the given channel + * Determine whether the given channel exists. */ - public function find(string $channel): ?Channel + public function exists(string $channel): bool { - return $this->channels($channel); + return isset($this->applications[$this->application->id()][$channel]); } /** - * Determine whether the given channel exists. + * Find the given channel */ - public function exists(string $channel): bool + public function find(string $channel): ?Channel { - return isset($this->applications[$this->application->id()][$channel]); + return $this->channels($channel); } /**