From ce0e9cc78d69dfa2a632bb492e53fa0a4f22b800 Mon Sep 17 00:00:00 2001 From: "Buster \"Silver Eagle\" Neece" Date: Sun, 16 Jan 2022 22:45:07 -0600 Subject: [PATCH] Introduce two new enums. --- config/services.php | 3 +- src/Assets/BrowserIconCustomAsset.php | 4 +-- src/Console/Command/InitializeCommand.php | 2 +- src/Controller/Admin/SettingsAction.php | 2 +- src/Controller/Frontend/SetupController.php | 2 +- src/Customization.php | 2 +- src/Enums/ApplicationEnvironment.php | 33 +++++++++++++++++++++ src/Enums/ReleaseChannel.php | 18 +++++++++++ src/Environment.php | 28 +++++++---------- src/Installer/EnvFiles/AzuraCastEnvFile.php | 7 ++--- src/Notification/Check/UpdateCheck.php | 7 +++-- src/Service/AzuraCastCentral.php | 8 ++--- src/Tests/Module.php | 3 +- src/Version.php | 17 +++++------ 14 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 src/Enums/ApplicationEnvironment.php create mode 100644 src/Enums/ReleaseChannel.php diff --git a/config/services.php b/config/services.php index b4172b3ea..670617098 100644 --- a/config/services.php +++ b/config/services.php @@ -231,7 +231,8 @@ return [ Environment $environment ) { $console = new App\Console\Application( - $environment->getAppName() . ' Command Line Tools (' . $environment->getAppEnvironment() . ')', + $environment->getAppName() . ' Command Line Tools (' + . $environment->getAppEnvironmentEnum()->getName() . ')', $version->getVersion() ); $console->setDispatcher($dispatcher); diff --git a/src/Assets/BrowserIconCustomAsset.php b/src/Assets/BrowserIconCustomAsset.php index 2d6e8b3fb..34873eed7 100644 --- a/src/Assets/BrowserIconCustomAsset.php +++ b/src/Assets/BrowserIconCustomAsset.php @@ -36,7 +36,7 @@ class BrowserIconCustomAsset extends AbstractCustomAsset protected function getDefaultUrl(): string { $assetUrl = $this->environment->getAssetUrl(); - return $assetUrl . '/icons/' . $this->environment->getAppEnvironment() . '/original.png'; + return $assetUrl . '/icons/' . $this->environment->getAppEnvironmentEnum()->value . '/original.png'; } public function upload(Image $image): void @@ -75,6 +75,6 @@ class BrowserIconCustomAsset extends AbstractCustomAsset return $assetUrl . '/uploads/browser_icon/' . $size . '.' . $mtime . '.png'; } - return $assetUrl . '/icons/' . $this->environment->getAppEnvironment() . '/' . $size . '.png'; + return $assetUrl . '/icons/' . $this->environment->getAppEnvironmentEnum()->value . '/' . $size . '.png'; } } diff --git a/src/Console/Command/InitializeCommand.php b/src/Console/Command/InitializeCommand.php index 802364799..e54eb725f 100644 --- a/src/Console/Command/InitializeCommand.php +++ b/src/Console/Command/InitializeCommand.php @@ -33,7 +33,7 @@ class InitializeCommand extends CommandAbstract $io->listing( [ - __('Environment: %s', ucfirst($this->environment->getAppEnvironment())), + __('Environment: %s', $this->environment->getAppEnvironmentEnum()->getName()), __('Installation Method: %s', $this->environment->isDocker() ? 'Docker' : 'Ansible'), ] ); diff --git a/src/Controller/Admin/SettingsAction.php b/src/Controller/Admin/SettingsAction.php index 3d7075601..117c28ea0 100644 --- a/src/Controller/Admin/SettingsAction.php +++ b/src/Controller/Admin/SettingsAction.php @@ -28,7 +28,7 @@ class SettingsAction 'apiUrl' => (string)$router->named('api:admin:settings', [ 'group' => Settings::GROUP_GENERAL, ]), - 'releaseChannel' => $version->getReleaseChannel(), + 'releaseChannel' => $version->getReleaseChannelEnum()->value, ], ); } diff --git a/src/Controller/Frontend/SetupController.php b/src/Controller/Frontend/SetupController.php index 24ebaf70a..fa0c9e21a 100644 --- a/src/Controller/Frontend/SetupController.php +++ b/src/Controller/Frontend/SetupController.php @@ -168,7 +168,7 @@ class SetupController 'apiUrl' => (string)$router->named('api:admin:settings', [ 'group' => Entity\Settings::GROUP_GENERAL, ]), - 'releaseChannel' => $version->getReleaseChannel(), + 'releaseChannel' => $version->getReleaseChannelEnum()->value, 'continueUrl' => (string)$router->named('dashboard'), ], ); diff --git a/src/Customization.php b/src/Customization.php index cfa0f10d4..7af2e174c 100644 --- a/src/Customization.php +++ b/src/Customization.php @@ -168,7 +168,7 @@ class Customization } if (!$this->environment->isProduction()) { - $title = '(' . ucfirst($this->environment->getAppEnvironment()) . ') ' . $title; + $title = '(' . $this->environment->getAppEnvironmentEnum()->getName() . ') ' . $title; } return $title ?? ''; diff --git a/src/Enums/ApplicationEnvironment.php b/src/Enums/ApplicationEnvironment.php new file mode 100644 index 000000000..fd37f7e75 --- /dev/null +++ b/src/Enums/ApplicationEnvironment.php @@ -0,0 +1,33 @@ +value); + } + + public static function default(): self + { + return self::Production; + } + + public static function toSelect(): array + { + $values = []; + foreach (self::cases() as $case) { + $values[$case->value] = $case->getName(); + } + return $values; + } +} diff --git a/src/Enums/ReleaseChannel.php b/src/Enums/ReleaseChannel.php new file mode 100644 index 000000000..7536cac0f --- /dev/null +++ b/src/Enums/ReleaseChannel.php @@ -0,0 +1,18 @@ + 'AzuraCast', - self::APP_ENV => self::ENV_PRODUCTION, self::LOG_LEVEL => LogLevel::NOTICE, self::IS_DOCKER => true, @@ -103,24 +99,25 @@ class Environment return $this->data; } - public function getAppEnvironment(): string + public function getAppEnvironmentEnum(): ApplicationEnvironment { - return $this->data[self::APP_ENV] ?? self::ENV_PRODUCTION; + return ApplicationEnvironment::tryFrom($this->data[self::APP_ENV] ?? '') + ?? ApplicationEnvironment::default(); } public function isProduction(): bool { - return self::ENV_PRODUCTION === $this->getAppEnvironment(); + return ApplicationEnvironment::Production === $this->getAppEnvironmentEnum(); } public function isTesting(): bool { - return self::ENV_TESTING === $this->getAppEnvironment(); + return ApplicationEnvironment::Testing === $this->getAppEnvironmentEnum(); } public function isDevelopment(): bool { - return self::ENV_DEVELOPMENT === $this->getAppEnvironment(); + return ApplicationEnvironment::Development === $this->getAppEnvironmentEnum(); } public function isDocker(): bool @@ -214,13 +211,10 @@ class Environment return $this->data[self::LANG]; } - public function getReleaseChannel(): string + public function getReleaseChannelEnum(): ReleaseChannel { - $channel = $this->data[self::RELEASE_CHANNEL] ?? 'latest'; - - return ('stable' === $channel) - ? Version::RELEASE_CHANNEL_STABLE - : Version::RELEASE_CHANNEL_ROLLING; + return ReleaseChannel::tryFrom($this->data[self::RELEASE_CHANNEL] ?? '') + ?? ReleaseChannel::default(); } public function getSftpPort(): int diff --git a/src/Installer/EnvFiles/AzuraCastEnvFile.php b/src/Installer/EnvFiles/AzuraCastEnvFile.php index b70ae7740..f07161b04 100644 --- a/src/Installer/EnvFiles/AzuraCastEnvFile.php +++ b/src/Installer/EnvFiles/AzuraCastEnvFile.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Installer\EnvFiles; +use App\Enums\ApplicationEnvironment; use App\Enums\SupportedLocales; use App\Environment; use Psr\Log\LogLevel; @@ -42,11 +43,7 @@ class AzuraCastEnvFile extends AbstractEnvFile 'name' => __( 'The application environment.', ), - 'options' => [ - Environment::ENV_PRODUCTION, - Environment::ENV_DEVELOPMENT, - Environment::ENV_TESTING, - ], + 'options' => ApplicationEnvironment::toSelect(), 'required' => true, ], Environment::LOG_LEVEL => [ diff --git a/src/Notification/Check/UpdateCheck.php b/src/Notification/Check/UpdateCheck.php index 11c21cb1e..3c931671c 100644 --- a/src/Notification/Check/UpdateCheck.php +++ b/src/Notification/Check/UpdateCheck.php @@ -6,6 +6,7 @@ namespace App\Notification\Check; use App\Entity; use App\Enums\GlobalPermissions; +use App\Enums\ReleaseChannel; use App\Event\GetNotifications; use App\Session\Flash; use App\Version; @@ -41,9 +42,9 @@ class UpdateCheck $actionLabel = __('Update Instructions'); $actionUrl = Version::UPDATE_URL; - $releaseChannel = $this->version->getReleaseChannel(); + $releaseChannel = $this->version->getReleaseChannelEnum(); - if (Version::RELEASE_CHANNEL_STABLE === $releaseChannel && $updateData['needs_release_update']) { + if (ReleaseChannel::Stable === $releaseChannel && $updateData['needs_release_update']) { $notificationParts = [ '' . __( 'AzuraCast version %s is now available.', @@ -67,7 +68,7 @@ class UpdateCheck return; } - if (Version::RELEASE_CHANNEL_ROLLING === $releaseChannel && $updateData['needs_rolling_update']) { + if (ReleaseChannel::RollingRelease === $releaseChannel && $updateData['needs_rolling_update']) { $notificationParts = []; $notificationParts[] = '' . __( diff --git a/src/Service/AzuraCastCentral.php b/src/Service/AzuraCastCentral.php index 630eee6c0..db3595b0d 100644 --- a/src/Service/AzuraCastCentral.php +++ b/src/Service/AzuraCastCentral.php @@ -32,10 +32,10 @@ class AzuraCastCentral public function checkForUpdates(): ?array { $request_body = [ - 'id' => $this->getUniqueIdentifier(), - 'is_docker' => $this->environment->isDocker(), - 'environment' => $this->environment->getAppEnvironment(), - 'release_channel' => $this->version->getReleaseChannel(), + 'id' => $this->getUniqueIdentifier(), + 'is_docker' => $this->environment->isDocker(), + 'environment' => $this->environment->getAppEnvironmentEnum()->value, + 'release_channel' => $this->version->getReleaseChannelEnum()->value, ]; $commit_hash = $this->version->getCommitHash(); diff --git a/src/Tests/Module.php b/src/Tests/Module.php index 7a86b3c63..017b60071 100644 --- a/src/Tests/Module.php +++ b/src/Tests/Module.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace App\Tests; use App\Doctrine\ReloadableEntityManagerInterface; +use App\Enums\ApplicationEnvironment; use App\Environment; use Codeception\Configuration; use Codeception\Lib\Framework; @@ -46,7 +47,7 @@ class Module extends Framework implements DoctrineProvider $autoloader, [ Environment::BASE_DIR => Configuration::projectDir(), - Environment::APP_ENV => Environment::ENV_TESTING, + Environment::APP_ENV => ApplicationEnvironment::Testing->value, ] ); diff --git a/src/Version.php b/src/Version.php index cc2b2d5ce..444576ddd 100644 --- a/src/Version.php +++ b/src/Version.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App; +use App\Enums\ReleaseChannel; use DateTime; use DateTimeZone; use Psr\SimpleCache\CacheInterface; @@ -17,9 +18,6 @@ class Version /** @var string Version that is displayed if no Git repository information is present. */ public const FALLBACK_VERSION = '0.15.0'; - public const RELEASE_CHANNEL_ROLLING = 'rolling'; - public const RELEASE_CHANNEL_STABLE = 'stable'; - // phpcs:disable Generic.Files.LineLength public const LATEST_COMPOSE_REVISION = 12; public const LATEST_COMPOSE_URL = 'https://raw.githubusercontent.com/AzuraCast/AzuraCast/main/docker-compose.sample.yml'; @@ -37,17 +35,16 @@ class Version $this->repoDir = $environment->getBaseDirectory(); } - public function getReleaseChannel(): string + public function getReleaseChannelEnum(): ReleaseChannel { if ($this->environment->isDocker()) { - return $this->environment->getReleaseChannel(); + return $this->environment->getReleaseChannelEnum(); } $details = $this->getDetails(); - return ('stable' === $details['branch']) - ? self::RELEASE_CHANNEL_STABLE - : self::RELEASE_CHANNEL_ROLLING; + ? ReleaseChannel::Stable + : ReleaseChannel::RollingRelease; } /** @@ -162,8 +159,8 @@ class Version $details['commit_date'] ); - $releaseChannel = $this->getReleaseChannel(); - if (self::RELEASE_CHANNEL_ROLLING === $releaseChannel) { + $releaseChannel = $this->getReleaseChannelEnum(); + if (ReleaseChannel::RollingRelease === $releaseChannel) { return 'Rolling Release ' . $commitText; } return 'v' . $details['tag'] . ' Stable';