From 279deeeb7c0f99d4fbc3fef8db78eb920d0aa49b Mon Sep 17 00:00:00 2001 From: freek Date: Sat, 1 Dec 2018 14:12:15 +0100 Subject: [PATCH] rename client to app --- src/Apps/App.php | 12 +++--- src/Apps/ConfigAppProvider.php | 12 +++--- src/Dashboard/DashboardLogger.php | 12 +++--- src/Server/Logger/WebsocketsLogger.php | 4 +- src/WebSockets/Channels/Channel.php | 2 +- src/WebSockets/Channels/ChannelManager.php | 12 +++--- src/WebSockets/Messages/Message.php | 2 +- src/WebSockets/Messages/PusherMessage.php | 4 +- src/WebSockets/WebSocketHandler.php | 6 +-- tests/Channels/PresenceChannelTest.php | 2 +- tests/Channels/PrivateChannelTest.php | 4 +- .../{ClientTest.php => AppTest.php} | 2 +- .../ClientProviders/ConfigAppProviderTest.php | 37 +++++++++++++++++++ .../ConfigClientProviderTest.php | 37 ------------------- tests/ConnectionTest.php | 12 +++--- tests/TestCase.php | 4 +- 16 files changed, 81 insertions(+), 83 deletions(-) rename tests/ClientProviders/{ClientTest.php => AppTest.php} (96%) create mode 100644 tests/ClientProviders/ConfigAppProviderTest.php delete mode 100644 tests/ClientProviders/ConfigClientProviderTest.php diff --git a/src/Apps/App.php b/src/Apps/App.php index 7cbcaeb..b569e0a 100644 --- a/src/Apps/App.php +++ b/src/Apps/App.php @@ -7,13 +7,13 @@ use BeyondCode\LaravelWebSockets\Exceptions\InvalidApp; class App { /** @var int */ - public $appId; + public $id; /** @var string */ - public $appKey; + public $key; /** @var string */ - public $appSecret; + public $secret; /** @var string|null */ public $name; @@ -42,11 +42,11 @@ class App throw InvalidApp::valueIsRequired('appSecret', $appId); } - $this->appId = $appId; + $this->id = $appId; - $this->appKey = $appKey; + $this->key = $appKey; - $this->appSecret = $appSecret; + $this->secret = $appSecret; $this->name = $name; } diff --git a/src/Apps/ConfigAppProvider.php b/src/Apps/ConfigAppProvider.php index 2a88829..89c0900 100644 --- a/src/Apps/ConfigAppProvider.php +++ b/src/Apps/ConfigAppProvider.php @@ -18,28 +18,28 @@ class ConfigAppProvider implements AppProvider public function all(): array { return $this->apps - ->map(function ($client) { - return $this->instanciate($client); + ->map(function (array $appAttributes) { + return $this->instanciate($appAttributes); }) ->toArray(); } public function findById(int $appId): ?App { - $clientAttributes = $this + $appAttributes = $this ->apps ->firstWhere('id', $appId); - return $this->instanciate($clientAttributes); + return $this->instanciate($appAttributes); } public function findByKey(string $appKey): ?App { - $clientAttributes = $this + $appAttributes = $this ->apps ->firstWhere('key', $appKey); - return $this->instanciate($clientAttributes); + return $this->instanciate($appAttributes); } protected function instanciate(?array $appAttributes): ?App diff --git a/src/Dashboard/DashboardLogger.php b/src/Dashboard/DashboardLogger.php index 39272d8..a15d495 100644 --- a/src/Dashboard/DashboardLogger.php +++ b/src/Dashboard/DashboardLogger.php @@ -22,7 +22,7 @@ class DashboardLogger /** @var \GuzzleHttp\Psr7\Request $request */ $request = $connection->httpRequest; - static::log($connection->client->appId, static::TYPE_CONNECTION, [ + static::log($connection->app->id, static::TYPE_CONNECTION, [ 'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}", 'socketId' => $connection->socketId, ]); @@ -30,14 +30,14 @@ class DashboardLogger public static function occupied(ConnectionInterface $connection, string $channelName) { - static::log($connection->client->appId, static::TYPE_OCCUPIED, [ + static::log($connection->app->id, static::TYPE_OCCUPIED, [ 'details' => "Channel: {$channelName}", ]); } public static function subscribed(ConnectionInterface $connection, string $channelName) { - static::log($connection->client->appId, static::TYPE_SUBSCRIBED, [ + static::log($connection->app->id, static::TYPE_SUBSCRIBED, [ 'socketId' => $connection->socketId, 'details' => "Channel: {$channelName}", ]); @@ -45,7 +45,7 @@ class DashboardLogger public static function clientMessage(ConnectionInterface $connection, stdClass $payload) { - static::log($connection->client->appId, static::TYPE_CLIENT_MESSAGE, [ + static::log($connection->app->id, static::TYPE_CLIENT_MESSAGE, [ 'details' => "Channel: {$payload->channel}, Event: {$payload->event}", 'socketId' => $connection->socketId, 'data' => json_encode($payload), @@ -54,14 +54,14 @@ class DashboardLogger public static function disconnection(ConnectionInterface $connection) { - static::log($connection->client->appId, static::TYPE_DISCONNECTION, [ + static::log($connection->app->id, static::TYPE_DISCONNECTION, [ 'socketId' => $connection->socketId, ]); } public static function vacated(ConnectionInterface $connection, string $channelName) { - static::log($connection->client->appId, static::TYPE_VACATED, [ + static::log($connection->app->id, static::TYPE_VACATED, [ 'details' => "Channel: {$channelName}", ]); } diff --git a/src/Server/Logger/WebsocketsLogger.php b/src/Server/Logger/WebsocketsLogger.php index 9444179..ec7aa9f 100644 --- a/src/Server/Logger/WebsocketsLogger.php +++ b/src/Server/Logger/WebsocketsLogger.php @@ -38,7 +38,7 @@ class WebsocketsLogger extends Logger implements MessageComponentInterface public function onMessage(ConnectionInterface $connection, MessageInterface $message) { - $this->info("{$connection->client->appId}: connection id {$connection->socketId} received message: {$message->getPayload()}."); + $this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}."); $this->app->onMessage(ConnectionLogger::decorate($connection), $message); } @@ -54,7 +54,7 @@ class WebsocketsLogger extends Logger implements MessageComponentInterface { $exceptionClass = get_class($exception); - $appId = $connection->client->appId ?? 'Unknown app id'; + $appId = $connection->app->id ?? 'Unknown app id'; $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index 757f965..23afc20 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -39,7 +39,7 @@ class Channel $signature .= ":{$payload->channel_data}"; } - if (str_after($payload->auth, ':') !== hash_hmac('sha256', $signature, $connection->client->appSecret)) { + if (str_after($payload->auth, ':') !== hash_hmac('sha256', $signature, $connection->app->secret)) { throw new InvalidSignature(); } } diff --git a/src/WebSockets/Channels/ChannelManager.php b/src/WebSockets/Channels/ChannelManager.php index e87d3c9..2353455 100644 --- a/src/WebSockets/Channels/ChannelManager.php +++ b/src/WebSockets/Channels/ChannelManager.php @@ -48,26 +48,26 @@ public function findOrCreate(string $appId, string $channelName): Channel public function removeFromAllChannels(ConnectionInterface $connection) { - if (!isset($connection->client)) { + if (!isset($connection->app)) { return; } /** * Remove the connection from all channels. */ - collect(array_get($this->channels, $connection->client->appId, []))->each->unsubscribe($connection); + collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection); /** * Unset all channels that have no connections so we don't leak memory. */ - collect(array_get($this->channels, $connection->client->appId, [])) + collect(array_get($this->channels, $connection->app->id, [])) ->reject->hasConnections() ->each(function (Channel $channel, string $channelName) use ($connection) { - unset($this->channels[$connection->client->appId][$channelName]); + unset($this->channels[$connection->app->id][$channelName]); }); - if (count(array_get($this->channels, $connection->client->appId, [])) === 0) { - unset($this->channels[$connection->client->appId]); + if (count(array_get($this->channels, $connection->app->id, [])) === 0) { + unset($this->channels[$connection->app->id]); }; } } \ No newline at end of file diff --git a/src/WebSockets/Messages/Message.php b/src/WebSockets/Messages/Message.php index 30b8422..f3a3166 100644 --- a/src/WebSockets/Messages/Message.php +++ b/src/WebSockets/Messages/Message.php @@ -36,7 +36,7 @@ class Message implements RespondableMessage DashboardLogger::clientMessage($this->connection, $this->payload); - $channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel); + $channel = $this->channelManager->find($this->connection->app->id, $this->payload->channel); optional($channel)->broadcast($this->payload); } diff --git a/src/WebSockets/Messages/PusherMessage.php b/src/WebSockets/Messages/PusherMessage.php index 50c6054..d861ba7 100644 --- a/src/WebSockets/Messages/PusherMessage.php +++ b/src/WebSockets/Messages/PusherMessage.php @@ -50,14 +50,14 @@ class PusherMessage implements RespondableMessage */ protected function subscribe(ConnectionInterface $connection, stdClass $payload) { - $channel = $this->channelManager->findOrCreate($connection->client->appId, $payload->channel); + $channel = $this->channelManager->findOrCreate($connection->app->id, $payload->channel); $channel->subscribe($connection, $payload); } public function unsubscribe(ConnectionInterface $connection, stdClass $payload) { - $channel = $this->channelManager->findOrCreate($connection->client->appId, $payload->channel); + $channel = $this->channelManager->findOrCreate($connection->app->id, $payload->channel); $channel->unsubscribe($connection); } diff --git a/src/WebSockets/WebSocketHandler.php b/src/WebSockets/WebSocketHandler.php index c859232..52c7291 100644 --- a/src/WebSockets/WebSocketHandler.php +++ b/src/WebSockets/WebSocketHandler.php @@ -58,11 +58,11 @@ class WebSocketHandler implements MessageComponentInterface { $appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); - if (!$client = App::findByKey($appKey)) { + if (!$app = App::findByKey($appKey)) { throw new UnknownAppKey($appKey); } - $connection->client = $client; + $connection->app = $app; return $this; } @@ -76,8 +76,6 @@ class WebSocketHandler implements MessageComponentInterface return $this; } - - protected function establishConnection(ConnectionInterface $connection) { $connection->send(json_encode([ diff --git a/tests/Channels/PresenceChannelTest.php b/tests/Channels/PresenceChannelTest.php index f44fd9f..def322b 100644 --- a/tests/Channels/PresenceChannelTest.php +++ b/tests/Channels/PresenceChannelTest.php @@ -47,7 +47,7 @@ class PresenceChannelTest extends TestCase $message = new Message(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => $connection->client->appKey.':'.hash_hmac('sha256', $signature, $connection->client->appSecret), + 'auth' => $connection->app->key.':'.hash_hmac('sha256', $signature, $connection->app->secret), 'channel' => 'presence-channel', 'channel_data' => json_encode($channelData) ], diff --git a/tests/Channels/PrivateChannelTest.php b/tests/Channels/PrivateChannelTest.php index 0937aeb..e0ee0d9 100644 --- a/tests/Channels/PrivateChannelTest.php +++ b/tests/Channels/PrivateChannelTest.php @@ -37,12 +37,12 @@ class PrivateChannelTest extends TestCase $signature = "{$connection->socketId}:private-channel"; - $hashedAppSecret = hash_hmac('sha256', $signature, $connection->client->appSecret); + $hashedAppSecret = hash_hmac('sha256', $signature, $connection->app->secret); $message = new Message(json_encode([ 'event' => 'pusher:subscribe', 'data' => [ - 'auth' => "{$connection->client->appKey}:{$hashedAppSecret}", + 'auth' => "{$connection->app->key}:{$hashedAppSecret}", 'channel' => 'private-channel' ], ])); diff --git a/tests/ClientProviders/ClientTest.php b/tests/ClientProviders/AppTest.php similarity index 96% rename from tests/ClientProviders/ClientTest.php rename to tests/ClientProviders/AppTest.php index 44a0220..2fc13eb 100644 --- a/tests/ClientProviders/ClientTest.php +++ b/tests/ClientProviders/AppTest.php @@ -6,7 +6,7 @@ use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\Exceptions\InvalidApp; use BeyondCode\LaravelWebSockets\Tests\TestCase; -class ClientTest extends TestCase +class AppTest extends TestCase { /** @test */ public function it_can_create_a_client() diff --git a/tests/ClientProviders/ConfigAppProviderTest.php b/tests/ClientProviders/ConfigAppProviderTest.php new file mode 100644 index 0000000..a15f771 --- /dev/null +++ b/tests/ClientProviders/ConfigAppProviderTest.php @@ -0,0 +1,37 @@ +configAppProvider = new ConfigAppProvider(); + } + + /** @test */ + public function it_can_get_client_from_the_config_file() + { + $apps = $this->configAppProvider->all(); + + $this->assertCount(1, $apps); + + /** @var $client */ + $client = $apps[0]; + + $this->assertEquals('Test App', $client->name); + $this->assertEquals(1234, $client->id); + $this->assertEquals('TestKey', $client->key); + $this->assertEquals('TestSecret', $client->secret); + + + } +} \ No newline at end of file diff --git a/tests/ClientProviders/ConfigClientProviderTest.php b/tests/ClientProviders/ConfigClientProviderTest.php deleted file mode 100644 index a6c31f7..0000000 --- a/tests/ClientProviders/ConfigClientProviderTest.php +++ /dev/null @@ -1,37 +0,0 @@ -configClientProvider = new ConfigAppProvider(); - } - - /** @test */ - public function it_can_get_client_from_the_config_file() - { - $clients = $this->configClientProvider->all(); - - $this->assertCount(1, $clients); - - /** @var $client */ - $client = $clients[0]; - - $this->assertEquals('Test Client', $client->name); - $this->assertEquals(1234, $client->appId); - $this->assertEquals('TestKey', $client->appKey); - $this->assertEquals('TestSecret', $client->appSecret); - - - } -} \ No newline at end of file diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index 14b66d2..caad118 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -28,17 +28,17 @@ class ConnectionTest extends TestCase } /** @test */ - public function successful_connections_have_the_client_attached() + public function successful_connections_have_the_app_attached() { $connection = $this->getWebSocketConnection(); $this->pusherServer->onOpen($connection); - $this->assertInstanceOf(App::class, $connection->client); - $this->assertSame(1234, $connection->client->appId); - $this->assertSame('TestKey', $connection->client->appKey); - $this->assertSame('TestSecret', $connection->client->appSecret); - $this->assertSame('Test Client', $connection->client->name); + $this->assertInstanceOf(App::class, $connection->app); + $this->assertSame(1234, $connection->app->id); + $this->assertSame('TestKey', $connection->app->key); + $this->assertSame('TestSecret', $connection->app->secret); + $this->assertSame('Test App', $connection->app->name); } /** @test */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 5c681ff..e4d28fe 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -36,7 +36,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase { $app['config']->set('websockets.clients', [ [ - 'name' => 'Test Client', + 'name' => 'Test App', 'id' => 1234, 'key' => 'TestKey', 'secret' => 'TestSecret' @@ -77,7 +77,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase protected function getChannel(ConnectionInterface $connection, string $channelName) { - return $this->channelManager->findOrCreate($connection->client->appId, $channelName); + return $this->channelManager->findOrCreate($connection->app->id, $channelName); } protected function markTestAsPassed()