diff --git a/config/websockets.php b/config/websockets.php index 5e639d3..490e72b 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -1,6 +1,6 @@ [ + 'apps' => [ [ + 'id' => env('PUSHER_APP_ID'), 'name' => env('APP_NAME'), - 'app_id' => env('PUSHER_APP_ID'), - 'app_key' => env('PUSHER_APP_KEY'), - 'app_secret' => env('PUSHER_APP_SECRET') + 'key' => env('PUSHER_APP_KEY'), + 'ecret' => env('PUSHER_APP_SECRET') ], ], @@ -26,7 +26,7 @@ return [ * You can create a custom provider by implementing the * `ClientProvier` interface. */ - 'client_provider' => ConfigClientProvider::class, + 'client_provider' => ConfigAppProvider::class, /* * This array contains the hosts of which you want to allow incoming requests. diff --git a/src/ClientProviders/Client.php b/src/Apps/App.php similarity index 72% rename from src/ClientProviders/Client.php rename to src/Apps/App.php index 3e4f985..75162ce 100644 --- a/src/ClientProviders/Client.php +++ b/src/Apps/App.php @@ -1,10 +1,10 @@ findByAppId($appId); + return app(AppProvider::class)->findById($appId); } - public static function findByAppKey(string $appKey): ?Client + public static function findByKey(string $appKey): ?App { - return app(ClientProvider::class)->findByAppKey($appKey); + return app(AppProvider::class)->findByKey($appKey); } public function __construct($appId, string $appKey, string $appSecret, ?string $name) diff --git a/src/Apps/AppProvider.php b/src/Apps/AppProvider.php new file mode 100644 index 0000000..aeab406 --- /dev/null +++ b/src/Apps/AppProvider.php @@ -0,0 +1,13 @@ +toArray(); } - public function findByAppId(int $appId): ?Client + + public function findById(int $appId): ?App { $clientAttributes = $this ->clients - ->firstWhere('app_id', $appId); + ->firstWhere('id', $appId); return $this->instanciate($clientAttributes); } - public function findByAppKey(string $appKey): ?Client + public function findByKey(string $appKey): ?App { $clientAttributes = $this ->clients - ->firstWhere('app_key', $appKey); + ->firstWhere('key', $appKey); return $this->instanciate($clientAttributes); } - protected function instanciate(?array $clientAttributes): ?Client + protected function instanciate(?array $clientAttributes): ?App { if (! $clientAttributes) { return null; } - return new Client( - $clientAttributes['app_id'], - $clientAttributes['app_key'], - $clientAttributes['app_secret'], + return new App( + $clientAttributes['id'], + $clientAttributes['key'], + $clientAttributes['secret'], $clientAttributes['name'] ?? null ); } diff --git a/src/ClientProviders/ClientProvider.php b/src/ClientProviders/ClientProvider.php deleted file mode 100644 index 4300092..0000000 --- a/src/ClientProviders/ClientProvider.php +++ /dev/null @@ -1,14 +0,0 @@ - $clients->all(), diff --git a/src/HttpApi/Controllers/Controller.php b/src/HttpApi/Controllers/Controller.php index b945056..5031251 100644 --- a/src/HttpApi/Controllers/Controller.php +++ b/src/HttpApi/Controllers/Controller.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers; -use BeyondCode\LaravelWebSockets\ClientProviders\Client; +use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; use BeyondCode\LaravelWebSockets\Events\ExceptionThrown; use BeyondCode\LaravelWebSockets\QueryParameters; @@ -77,7 +77,7 @@ abstract class Controller implements HttpServerInterface public function ensureValidAppId(string $appId) { - if (!$client = Client::findByAppId($appId)) { + if (!$client = App::findById($appId)) { throw new HttpException(401, "Unknown app id `{$appId}` provided."); } @@ -95,7 +95,7 @@ abstract class Controller implements HttpServerInterface "&auth_version={$request->get('auth_version')}" . "&body_md5={$bodyMd5}"; - $authSignature = hash_hmac('sha256', $signature, Client::findByAppId($request->get('appId'))->appSecret); + $authSignature = hash_hmac('sha256', $signature, App::findById($request->get('appId'))->appSecret); if ($authSignature !== $request->get('auth_signature')) { throw new HttpException(401, 'Invalid auth signature provided.'); diff --git a/src/WebSockets/WebSocketHandler.php b/src/WebSockets/WebSocketHandler.php index 0eb4db7..c859232 100644 --- a/src/WebSockets/WebSocketHandler.php +++ b/src/WebSockets/WebSocketHandler.php @@ -10,7 +10,7 @@ use BeyondCode\LaravelWebSockets\QueryParameters; use Exception; use Ratchet\ConnectionInterface; use Ratchet\RFC6455\Messaging\MessageInterface; -use BeyondCode\LaravelWebSockets\ClientProviders\Client; +use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; use Ratchet\WebSocket\MessageComponentInterface; @@ -58,7 +58,7 @@ class WebSocketHandler implements MessageComponentInterface { $appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); - if (!$client = Client::findByAppKey($appKey)) { + if (!$client = App::findByKey($appKey)) { throw new UnknownAppKey($appKey); } diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 4c73665..10d7470 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -9,7 +9,7 @@ use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize; use BeyondCode\LaravelWebSockets\Server\Router; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; -use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider; +use BeyondCode\LaravelWebSockets\Apps\AppProvider; use Illuminate\Support\ServiceProvider; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; @@ -44,7 +44,7 @@ class WebSocketsServiceProvider extends ServiceProvider return new ChannelManager(); }); - $this->app->singleton(ClientProvider::class, function() { + $this->app->singleton(AppProvider::class, function() { return app(config('websockets.client_provider')); }); } diff --git a/tests/ClientProviders/ClientTest.php b/tests/ClientProviders/ClientTest.php index 10c1c7a..f00ffc9 100644 --- a/tests/ClientProviders/ClientTest.php +++ b/tests/ClientProviders/ClientTest.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders; -use BeyondCode\LaravelWebSockets\ClientProviders\Client; +use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\Exceptions\InvalidClient; use BeyondCode\LaravelWebSockets\Tests\TestCase; @@ -11,7 +11,7 @@ class ClientTest extends TestCase /** @test */ public function it_can_create_a_client() { - new Client(1, 'appKey', 'appSecret', 'new'); + new App(1, 'appKey', 'appSecret', 'new'); $this->markTestAsPassed(); } @@ -21,7 +21,7 @@ class ClientTest extends TestCase { $this->expectException(InvalidClient::class); - new Client(1, '', 'appSecret', 'new'); + new App(1, '', 'appSecret', 'new'); } /** @test */ @@ -29,7 +29,7 @@ class ClientTest extends TestCase { $this->expectException(InvalidClient::class); - new Client(1, 'appKey', '', 'new'); + new App(1, 'appKey', '', 'new'); } /** @test */ @@ -37,6 +37,6 @@ class ClientTest extends TestCase { $this->expectException(InvalidClient::class); - new Client('appId', 'appKey', '', 'new'); + new App('appId', 'appKey', '', 'new'); } } \ No newline at end of file diff --git a/tests/ClientProviders/ConfigClientProviderTest.php b/tests/ClientProviders/ConfigClientProviderTest.php index b06e553..a6c31f7 100644 --- a/tests/ClientProviders/ConfigClientProviderTest.php +++ b/tests/ClientProviders/ConfigClientProviderTest.php @@ -2,19 +2,19 @@ namespace BeyondCode\LaravelWebSockets\Tests\ClientProviders; -use BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider; +use BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider; use BeyondCode\LaravelWebSockets\Tests\TestCase; class ConfigClientProviderTest extends TestCase { - /** @var \BeyondCode\LaravelWebSockets\ClientProviders\ConfigClientProvider */ + /** @var \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider */ protected $configClientProvider; public function setUp() { parent::setUp(); - $this->configClientProvider = new ConfigClientProvider(); + $this->configClientProvider = new ConfigAppProvider(); } /** @test */ diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index c92ff80..14b66d2 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Tests; -use BeyondCode\LaravelWebSockets\ClientProviders\Client; +use BeyondCode\LaravelWebSockets\Apps\App; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; @@ -34,7 +34,7 @@ class ConnectionTest extends TestCase $this->pusherServer->onOpen($connection); - $this->assertInstanceOf(Client::class, $connection->client); + $this->assertInstanceOf(App::class, $connection->client); $this->assertSame(1234, $connection->client->appId); $this->assertSame('TestKey', $connection->client->appKey); $this->assertSame('TestSecret', $connection->client->appSecret); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5b2188d..2ea8360 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -38,9 +38,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase $app['config']->set('websockets.clients', [ [ 'name' => 'Test Client', - 'app_id' => 1234, - 'app_key' => 'TestKey', - 'app_secret' => 'TestSecret' + 'id' => 1234, + 'key' => 'TestKey', + 'secret' => 'TestSecret' ] ]); }