Skip to content

Inject UrlBuilder instance into the BigBlueButton constructor #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 11 additions & 26 deletions src/BigBlueButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,20 @@ class BigBlueButton
/**
* @param null|array<string, mixed> $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,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For full BC support, the parameter must be appended. We cannot drop or replace the other parameters.

For a smooth way to the next major version, without disruptive BC breaks, we can (in a follow-up) deprecate the constructor and recommend to use dedicated static factory methods.

) {
$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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need these getters if we want to fill the deprecated properties.
We can remove all of this in the next major version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, maybe we can leave the properties uninitialized?

Any code that currently uses these properties would do so by extending BigBlueButton.
It is unlikely that the same code would also want to inject a UrlBuilder class.
Unless we are talking about two separate packages.


$this->urlBuilder = new UrlBuilder($bbbSecret, $bbbBaseUrl, $hashingAlgorithm);
$this->urlBuilder = $urlBuilder;
$this->curlOpts = $opts['curl'] ?? [];
}

Expand Down
62 changes: 62 additions & 0 deletions src/Util/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to decide whether the env vars should be still supported in the next major version.
Until that is decided, this static factory can remain @internal.


// Getters & Setters
public function setSecret(string $secret): self
{
Expand All @@ -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
Expand All @@ -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;
Expand Down