2018-11-24 00:25:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
2018-12-01 12:57:02 +00:00
|
|
|
namespace BeyondCode\LaravelWebSockets\Apps;
|
2018-11-24 00:25:40 +00:00
|
|
|
|
2018-11-24 00:53:20 +00:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
2018-12-01 12:57:02 +00:00
|
|
|
class ConfigAppProvider implements AppProvider
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2018-11-28 08:53:03 +00:00
|
|
|
/** @var Collection */
|
|
|
|
|
protected $clients;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->clients = collect(config('websockets.clients'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-28 22:59:58 +00:00
|
|
|
/** @return array[\BeyondCode\LaravelWebSockets\ClientProviders\Client] */
|
2018-11-25 23:29:35 +00:00
|
|
|
public function all(): array
|
|
|
|
|
{
|
2018-11-28 08:53:03 +00:00
|
|
|
return $this->clients
|
2018-11-25 23:29:35 +00:00
|
|
|
->map(function ($client) {
|
|
|
|
|
return $this->instanciate($client);
|
|
|
|
|
})
|
|
|
|
|
->toArray();
|
|
|
|
|
}
|
2018-12-01 12:57:02 +00:00
|
|
|
|
|
|
|
|
public function findById(int $appId): ?App
|
2018-11-24 00:53:20 +00:00
|
|
|
{
|
|
|
|
|
$clientAttributes = $this
|
2018-11-28 08:53:03 +00:00
|
|
|
->clients
|
2018-12-01 12:57:02 +00:00
|
|
|
->firstWhere('id', $appId);
|
2018-11-24 00:53:20 +00:00
|
|
|
|
|
|
|
|
return $this->instanciate($clientAttributes);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 12:57:02 +00:00
|
|
|
public function findByKey(string $appKey): ?App
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2018-11-24 00:53:20 +00:00
|
|
|
$clientAttributes = $this
|
2018-11-28 08:53:03 +00:00
|
|
|
->clients
|
2018-12-01 12:57:02 +00:00
|
|
|
->firstWhere('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-12-01 12:57:02 +00:00
|
|
|
protected function instanciate(?array $clientAttributes): ?App
|
2018-11-24 00:53:20 +00:00
|
|
|
{
|
|
|
|
|
if (! $clientAttributes) {
|
2018-11-24 00:25:40 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 12:57:02 +00:00
|
|
|
return new App(
|
|
|
|
|
$clientAttributes['id'],
|
|
|
|
|
$clientAttributes['key'],
|
|
|
|
|
$clientAttributes['secret'],
|
2018-11-24 22:52:55 +00:00
|
|
|
$clientAttributes['name'] ?? null
|
2018-11-24 00:25:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|