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
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
use BeyondCode\LaravelWebSockets\Contracts\AppManager;
|
|
|
|
|
|
2020-08-13 11:51:18 +00:00
|
|
|
class ConfigAppManager implements AppManager
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The list of apps.
|
|
|
|
|
*
|
|
|
|
|
* @var \Illuminate\Support\Collection
|
|
|
|
|
*/
|
2018-12-01 12:58:00 +00:00
|
|
|
protected $apps;
|
2018-11-28 08:53:03 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Initialize the class.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-28 08:53:03 +00:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
2018-12-01 13:17:32 +00:00
|
|
|
$this->apps = collect(config('websockets.apps'));
|
2018-11-28 08:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get all apps.
|
|
|
|
|
*
|
|
|
|
|
* @return array[\BeyondCode\LaravelWebSockets\Apps\App]
|
|
|
|
|
*/
|
2018-11-25 23:29:35 +00:00
|
|
|
public function all(): array
|
|
|
|
|
{
|
2020-12-07 18:48:15 +00:00
|
|
|
return $this->apps->map(function (array $appAttributes) {
|
|
|
|
|
return $this->convertIntoApp($appAttributes);
|
|
|
|
|
})->toArray();
|
2018-11-25 23:29:35 +00:00
|
|
|
}
|
2018-12-01 12:57:02 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get app by id.
|
|
|
|
|
*
|
2020-09-04 13:23:43 +00:00
|
|
|
* @param string|int $appId
|
2020-08-18 17:21:22 +00:00
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
|
|
|
|
|
*/
|
2018-12-03 09:06:40 +00:00
|
|
|
public function findById($appId): ?App
|
2018-11-24 00:53:20 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
return $this->convertIntoApp(
|
|
|
|
|
$this->apps->firstWhere('id', $appId)
|
|
|
|
|
);
|
2018-11-24 00:53:20 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get app by app key.
|
|
|
|
|
*
|
2020-09-04 01:11:45 +00:00
|
|
|
* @param string $appKey
|
2020-08-18 17:21:22 +00:00
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
|
|
|
|
|
*/
|
|
|
|
|
public function findByKey($appKey): ?App
|
2018-11-24 00:25:40 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
return $this->convertIntoApp(
|
|
|
|
|
$this->apps->firstWhere('key', $appKey)
|
|
|
|
|
);
|
2018-11-24 00:53:20 +00:00
|
|
|
}
|
2018-11-24 00:25:40 +00:00
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Get app by secret.
|
|
|
|
|
*
|
2020-09-04 01:11:45 +00:00
|
|
|
* @param string $appSecret
|
2020-08-18 17:21:22 +00:00
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
|
|
|
|
|
*/
|
|
|
|
|
public function findBySecret($appSecret): ?App
|
2018-12-04 09:15:37 +00:00
|
|
|
{
|
2020-09-10 19:59:26 +00:00
|
|
|
return $this->convertIntoApp(
|
|
|
|
|
$this->apps->firstWhere('secret', $appSecret)
|
|
|
|
|
);
|
2018-12-04 09:15:37 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Map the app into an App instance.
|
|
|
|
|
*
|
|
|
|
|
* @param array|null $app
|
|
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
|
|
|
|
|
*/
|
2020-09-10 19:59:26 +00:00
|
|
|
protected function convertIntoApp(?array $appAttributes): ?App
|
2018-11-24 00:53:20 +00:00
|
|
|
{
|
2018-12-04 21:22:33 +00:00
|
|
|
if (! $appAttributes) {
|
2018-11-24 00:25:40 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:47:55 +00:00
|
|
|
$app = new App(
|
2018-12-01 12:58:00 +00:00
|
|
|
$appAttributes['id'],
|
|
|
|
|
$appAttributes['key'],
|
2018-12-01 14:47:55 +00:00
|
|
|
$appAttributes['secret']
|
2018-11-24 00:25:40 +00:00
|
|
|
);
|
2018-12-01 14:47:55 +00:00
|
|
|
|
|
|
|
|
if (isset($appAttributes['name'])) {
|
|
|
|
|
$app->setName($appAttributes['name']);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-05 12:58:30 +00:00
|
|
|
if (isset($appAttributes['host'])) {
|
|
|
|
|
$app->setHost($appAttributes['host']);
|
2018-12-05 11:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-24 11:24:01 +00:00
|
|
|
if (isset($appAttributes['path'])) {
|
|
|
|
|
$app->setPath($appAttributes['path']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-07 18:48:15 +00:00
|
|
|
$app->enableClientMessages($appAttributes['enable_client_messages'])
|
2019-05-11 06:48:33 +00:00
|
|
|
->enableStatistics($appAttributes['enable_statistics'])
|
2020-08-18 13:04:52 +00:00
|
|
|
->setCapacity($appAttributes['capacity'] ?? null)
|
|
|
|
|
->setAllowedOrigins($appAttributes['allowed_origins'] ?? []);
|
2018-12-03 10:58:42 +00:00
|
|
|
|
2018-12-01 14:47:55 +00:00
|
|
|
return $app;
|
2018-11-24 00:25:40 +00:00
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|