rename client to app

This commit is contained in:
freek 2018-12-01 14:12:15 +01:00
parent 9e7d6f2369
commit 279deeeb7c
16 changed files with 81 additions and 83 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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}",
]);
}

View File

@ -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()}`.";

View File

@ -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();
}
}

View File

@ -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]);
};
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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([

View File

@ -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)
],

View File

@ -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'
],
]));

View File

@ -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()

View File

@ -0,0 +1,37 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders;
use BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class ConfigAppProviderTest extends TestCase
{
/** @var \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider */
protected $configAppProvider;
public function setUp()
{
parent::setUp();
$this->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);
}
}

View File

@ -1,37 +0,0 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders;
use BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider;
use BeyondCode\LaravelWebSockets\Tests\TestCase;
class ConfigClientProviderTest extends TestCase
{
/** @var \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider */
protected $configClientProvider;
public function setUp()
{
parent::setUp();
$this->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);
}
}

View File

@ -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 */

View File

@ -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()