laravel-websockets/src/ClientProviders/ConfigClientProvider.php

54 lines
1.3 KiB
PHP
Raw Normal View History

2018-11-24 00:25:40 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\ClientProviders;
2018-11-24 00:53:20 +00:00
use Illuminate\Support\Collection;
2018-11-24 00:25:40 +00:00
class ConfigClientProvider implements ClientProvider
{
2018-11-24 00:53:20 +00:00
public function findByAppId(int $appId): ?Client
{
$clientAttributes = $this
->allClients()
->firstWhere('app_id', $appId);
return $this->instanciate($clientAttributes);
}
public function findByAppKey(string $appKey): ?Client
2018-11-24 00:25:40 +00:00
{
2018-11-24 00:53:20 +00:00
$clientAttributes = $this
->allClients()
->firstWhere('app_key', $appKey);
2018-11-24 00:25:40 +00:00
2018-11-24 00:53:20 +00:00
return $this->instanciate($clientAttributes);
}
2018-11-24 00:25:40 +00:00
2018-11-24 22:52:55 +00:00
public function all(): array
{
return $this->allClients()
->map(function ($client) {
return $this->instanciate($client);
})
->toArray();
}
2018-11-24 00:53:20 +00:00
protected function allClients(): Collection
{
return collect(config('websockets.clients'));
}
protected function instanciate(?array $clientAttributes): ?Client
{
if (! $clientAttributes) {
2018-11-24 00:25:40 +00:00
return null;
}
return new Client(
2018-11-24 00:53:20 +00:00
$clientAttributes['app_id'],
$clientAttributes['app_key'],
2018-11-24 22:52:55 +00:00
$clientAttributes['app_secret'],
$clientAttributes['name'] ?? null
2018-11-24 00:25:40 +00:00
);
}
}