laravel-websockets/src/Apps/App.php

187 lines
3.6 KiB
PHP
Raw Normal View History

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-12-01 12:58:25 +00:00
use BeyondCode\LaravelWebSockets\Exceptions\InvalidApp;
2018-11-24 00:25:40 +00:00
2018-12-01 12:57:02 +00:00
class App
2018-11-24 00:25:40 +00:00
{
2020-09-04 13:23:03 +00:00
/** @var string|int */
2018-12-01 13:12:15 +00:00
public $id;
2018-11-24 00:25:40 +00:00
/** @var string */
2018-12-01 13:12:15 +00:00
public $key;
2018-11-24 00:25:40 +00:00
/** @var string */
2018-12-01 13:12:15 +00:00
public $secret;
2018-11-24 00:25:40 +00:00
2018-11-24 22:52:55 +00:00
/** @var string|null */
public $name;
2018-12-05 11:36:02 +00:00
/** @var string|null */
2018-12-05 12:58:30 +00:00
public $host;
2018-12-05 11:36:02 +00:00
/** @var string|null */
public $path;
/** @var int|null */
public $capacity = null;
2018-12-01 14:47:55 +00:00
/** @var bool */
public $clientMessagesEnabled = false;
2018-12-03 10:58:42 +00:00
/** @var bool */
public $statisticsEnabled = true;
2020-08-18 13:04:52 +00:00
/** @var array */
public $allowedOrigins = [];
2020-08-18 17:21:22 +00:00
/**
* Find the app by id.
*
2020-09-04 13:23:03 +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 static function findById($appId)
2018-11-24 00:25:40 +00:00
{
2020-08-13 11:51:18 +00:00
return app(AppManager::class)->findById($appId);
2018-11-24 00:53:20 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Find the app by app key.
*
* @param string $appKey
2020-08-18 17:21:22 +00:00
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
*/
public static function findByKey($appKey): ?self
2018-11-24 00:53:20 +00:00
{
2020-08-13 11:51:18 +00:00
return app(AppManager::class)->findByKey($appKey);
2018-11-24 00:25:40 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Find the app by app secret.
*
* @param string $appSecret
2020-08-18 17:21:22 +00:00
* @return \BeyondCode\LaravelWebSockets\Apps\App|null
*/
public static function findBySecret($appSecret): ?self
2018-12-04 09:15:37 +00:00
{
2020-08-13 11:51:18 +00:00
return app(AppManager::class)->findBySecret($appSecret);
2018-12-04 09:15:37 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Initialize the Web Socket app instance.
*
2020-09-04 13:23:03 +00:00
* @param string|int $appId
2020-09-04 13:53:42 +00:00
* @param string $key
* @param string $secret
2020-08-18 17:21:22 +00:00
* @return void
* @throws \BeyondCode\LaravelWebSockets\Exceptions\InvalidApp
*/
public function __construct($appId, $appKey, $appSecret)
2018-11-24 00:25:40 +00:00
{
if ($appKey === '') {
2018-12-01 12:58:25 +00:00
throw InvalidApp::valueIsRequired('appKey', $appId);
2018-11-24 00:25:40 +00:00
}
if ($appSecret === '') {
2018-12-01 12:58:25 +00:00
throw InvalidApp::valueIsRequired('appSecret', $appId);
2018-11-24 00:25:40 +00:00
}
2018-12-01 13:12:15 +00:00
$this->id = $appId;
$this->key = $appKey;
$this->secret = $appSecret;
2018-12-01 14:47:55 +00:00
}
2020-08-18 17:21:22 +00:00
/**
* Set the name of the app.
*
* @param string $appName
* @return $this
*/
2018-12-01 14:47:55 +00:00
public function setName(string $appName)
{
$this->name = $appName;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Set the app host.
*
* @param string $host
* @return $this
*/
2018-12-05 12:58:30 +00:00
public function setHost(string $host)
2018-12-05 11:36:02 +00:00
{
2018-12-05 12:58:30 +00:00
$this->host = $host;
2018-12-05 11:36:02 +00:00
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Set path for the app.
*
* @param string $path
* @return $this
*/
public function setPath(string $path)
{
$this->path = $path;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Enable client messages.
*
* @param bool $enabled
* @return $this
*/
2018-12-01 14:47:55 +00:00
public function enableClientMessages(bool $enabled = true)
{
$this->clientMessagesEnabled = $enabled;
2018-11-24 22:52:55 +00:00
2018-12-01 14:47:55 +00:00
return $this;
2018-11-24 00:25:40 +00:00
}
2018-12-03 10:58:42 +00:00
2020-08-18 17:21:22 +00:00
/**
* Set the maximum capacity for the app.
*
* @param int|null $capacity
* @return $this
*/
public function setCapacity(?int $capacity)
{
$this->capacity = $capacity;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Enable statistics for the app.
*
* @param bool $enabled
* @return $this
*/
2018-12-03 10:58:42 +00:00
public function enableStatistics(bool $enabled = true)
{
$this->statisticsEnabled = $enabled;
return $this;
}
2020-08-18 13:04:52 +00:00
2020-08-18 17:21:22 +00:00
/**
* Add whitelisted origins.
*
* @param array $allowedOrigins
* @return $this
*/
2020-08-18 13:04:52 +00:00
public function setAllowedOrigins(array $allowedOrigins)
{
$this->allowedOrigins = $allowedOrigins;
return $this;
}
2018-11-25 23:29:35 +00:00
}