diff --git a/src/BigBlueButton.php b/src/BigBlueButton.php index 24ebec9c..64d43e94 100644 --- a/src/BigBlueButton.php +++ b/src/BigBlueButton.php @@ -92,35 +92,20 @@ class BigBlueButton /** * @param null|array $opts */ - public function __construct(?string $baseUrl = null, ?string $secret = null, ?array $opts = []) - { - // Provide an early error message if configuration is wrong - if (is_null($baseUrl) && false === getenv('BBB_SERVER_BASE_URL')) { - throw new \RuntimeException('No BBB-Server-Url found! Please provide it either in constructor ' - . "(1st argument) or by environment variable 'BBB_SERVER_BASE_URL'!"); - } - - if (is_null($secret) && false === getenv('BBB_SECRET') && false === getenv('BBB_SECURITY_SALT')) { - throw new \RuntimeException('No BBB-Secret (or BBB-Salt) found! Please provide it either in constructor ' - . "(2nd argument) or by environment variable 'BBB_SECRET' (or 'BBB_SECURITY_SALT')!"); - } - - // Keeping backward compatibility with older deployed versions - // BBB_SECRET is the new variable name and have higher priority against the old named BBB_SECURITY_SALT - // Reminder: getenv() will return FALSE if not set. But bool is not accepted by $this->bbbSecret - // nor $this->bbbBaseUrl (only strings), thus FALSE will be converted automatically to an empty - // string (''). Having a bool should be not possible due to the checks above and the automated - // conversion, but PHPStan is still unhappy, so it's covered explicit by adding `?: ''`. - $bbbBaseUrl = $baseUrl ?: getenv('BBB_SERVER_BASE_URL') ?: ''; - $bbbSecret = $secret ?: getenv('BBB_SECRET') ?: getenv('BBB_SECURITY_SALT') ?: ''; - $hashingAlgorithm = HashingAlgorithm::SHA_256; + public function __construct( + ?string $baseUrl = null, + ?string $secret = null, + ?array $opts = [], + ?UrlBuilder $urlBuilder = null, + ) { + $urlBuilder ??= UrlBuilder::fromEnvVars($baseUrl, $secret); // initialize deprecated properties - $this->bbbBaseUrl = $bbbBaseUrl; - $this->bbbSecret = $bbbSecret; - $this->hashingAlgorithm = $hashingAlgorithm; + $this->bbbBaseUrl = $urlBuilder->getBaseUrl(); + $this->bbbSecret = $urlBuilder->getSecret(); + $this->hashingAlgorithm = $urlBuilder->getHashingAlgorithm(); - $this->urlBuilder = new UrlBuilder($bbbSecret, $bbbBaseUrl, $hashingAlgorithm); + $this->urlBuilder = $urlBuilder; $this->curlOpts = $opts['curl'] ?? []; } diff --git a/src/Util/UrlBuilder.php b/src/Util/UrlBuilder.php index 5a537af6..b4a7abb7 100644 --- a/src/Util/UrlBuilder.php +++ b/src/Util/UrlBuilder.php @@ -54,6 +54,42 @@ public function __construct(string $secret, string $baseUrl, HashingAlgorithm $h $this->setHashingAlgorithm($hashingAlgorithm); } + /** + * Creates a new instance from environment variables. + * + * The optional parameters allow to specify some of the values, while the + * remaining values fall back to environment variables or to a default + * value. + * + * This method only exists to BC-support creating a BigBlueButton class + * without injecting the UrlBuilder instance. + * + * @internal + */ + public static function fromEnvVars( + ?string $secret = null, + ?string $baseUrl = null, + ?HashingAlgorithm $hashingAlgorithm = null, + ): static { + $secret ??= getenv('BBB_SECRET') ?: getenv('BBB_SECURITY_SALT'); + if (false === $secret) { + throw new \RuntimeException("No BBB-Secret (or BBB-Salt) found! Please provide it either in constructor (2nd argument) or by environment variable 'BBB_SECRET' (or 'BBB_SECURITY_SALT')!"); + } + + $baseUrl ??= getenv('BBB_SERVER_BASE_URL'); + if (false === $baseUrl) { + throw new \RuntimeException('No BBB-Server-Url found! Please provide it either in constructor ' + . "(1st argument) or by environment variable 'BBB_SERVER_BASE_URL'!"); + } + + $hashingAlgorithm ??= HashingAlgorithm::SHA_256; + + // Extending classes need to override this method, if they change the + // constructor signature. + // @phpstan-ignore new.static + return new static($secret, $baseUrl, $hashingAlgorithm); + } + // Getters & Setters public function setSecret(string $secret): self { @@ -62,6 +98,19 @@ public function setSecret(string $secret): self return $this; } + /** + * Gets the secret. + * + * This method only exists to support a deprecated property in the + * BigBlueButton class. + * + * @internal + */ + public function getSecret(): string + { + return $this->secret; + } + public function setBaseUrl(string $baseUrl): self { // add tailing dir-separator if missing @@ -74,6 +123,19 @@ public function setBaseUrl(string $baseUrl): self return $this; } + /** + * Gets the base url. + * + * This method only exists to support a deprecated property in the + * BigBlueButton class. + * + * @internal + */ + public function getBaseUrl(): string + { + return $this->baseUrl; + } + public function setHashingAlgorithm(HashingAlgorithm $hashingAlgorithm): self { $this->hashingAlgorithm = $hashingAlgorithm;