From be4752197cbde2d296854f97cb774640b5e850e6 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 15 Aug 2022 15:51:34 +0200 Subject: [PATCH] Use enums for status codes --- src/MDConsole.php | 30 ++++++++---------------------- src/MDConsoleStatus.php | 32 ++++++++++++++++++++++++++++++++ src/MDOutputHandler.php | 37 +++++++++++-------------------------- 3 files changed, 51 insertions(+), 48 deletions(-) create mode 100644 src/MDConsoleStatus.php diff --git a/src/MDConsole.php b/src/MDConsole.php index bf78a02..6516538 100644 --- a/src/MDConsole.php +++ b/src/MDConsole.php @@ -11,20 +11,6 @@ declare(strict_types = 1); */ final class MDConsole { - const STATUS_NOTICE = 0; - const STATUS_UPDATE = 1; - const STATUS_DELETION = 2; - const STATUS_ERROR = 3; - const STATUS_SKIPPED = 4; - - const CONSOLE_LEVEL_COLORS = [ - self::STATUS_NOTICE => "", - self::STATUS_ERROR => "41", // 0: Red background | Problem - self::STATUS_UPDATE => "42", // 1: Green background | Edited automatically - self::STATUS_DELETION => "44", // 2: Blue background | Deleted - self::STATUS_SKIPPED => "40", // 2: Blue background | Deleted - ]; - const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1; const CONSOLE_LOG_USED_RESOURCES_DISABLED = 0; @@ -81,17 +67,17 @@ final class MDConsole { /** * Formats a message uniformly, maybe also to stdout. * - * @param string $context Context this is called from. - * @param integer $level Level of the notification. - * @param integer $editedNo Number of edited entries. - * @param string $additionalNote Optional, additional note. - * @param string $link Link. + * @param string $context Context this is called from. + * @param MDConsoleStatus $level Level of the notification. + * @param integer $editedNo Number of edited entries. + * @param string $additionalNote Optional, additional note. + * @param string $link Link. * * @return void */ - final public static function write(string $context, int $level = self::STATUS_NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void { + final public static function write(string $context, MDConsoleStatus $level = MDConsoleStatus::NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void { - if (self::$verbose === false and $level === self::STATUS_NOTICE) return; + if (self::$verbose === false and $level === MDConsoleStatus::NOTICE) return; // Get parts for generating the message to log. $additionalNote = \strtr($additionalNote, ["Γ€" => "ae", "ΓΆ" => "oe", "ΓΌ" => "ue"]); @@ -100,7 +86,7 @@ final class MDConsole { // Generate the message to log. # $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL; - $levelColor = self::CONSOLE_LEVEL_COLORS[$level]; + $levelColor = $level->color(); if ($link === "") { echo \sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m πŸ“ %-50s \033[40m::\033[0m πŸ“… %+20s\n", $context, $editedNo, $additionalNote, $date); diff --git a/src/MDConsoleStatus.php b/src/MDConsoleStatus.php new file mode 100644 index 0000000..d1c8cb1 --- /dev/null +++ b/src/MDConsoleStatus.php @@ -0,0 +1,32 @@ + + */ + declare(strict_types = 1); + +enum MDConsoleStatus { + case NOTICE; + case UPDATE; + case DELETION; + case ERROR; + case SKIPPED; + + /** + * Returns the color for each status. + * + * @return string + */ + public function color():string { + + return match($this) { + self::NOTICE => "", + self::ERROR => "41", // 0: Red background | Problem + self::UPDATE => "42", // 1: Green background | Edited automatically + self::DELETION => "44", // 2: Blue background | Deleted + self::SKIPPED => "40", // 2: Blue background | Deleted + }; + + } +} diff --git a/src/MDOutputHandler.php b/src/MDOutputHandler.php index f3a86c1..366e1a2 100644 --- a/src/MDOutputHandler.php +++ b/src/MDOutputHandler.php @@ -119,39 +119,24 @@ final class MDOutputHandler { * Main function of this class. * Formats a message uniformly and writes it to the log, maybe also to stdout. * - * @param string $msg Message to log. - * @param integer $statusCode Status code. - * 0: Note - * 1: Skipped - * 2: Update - * 3: Error. + * @param string $msg Message to log. + * @param MDConsoleStatus $statusCode Status code. * * @return void */ - public function toLog(string $msg, int $statusCode = 0):void { + public function toLog(string $msg, MDConsoleStatus $statusCode = MDConsoleStatus::NOTICE):void { // Get parts for generating the message to log. ++$this->_counter; // Increment counter $date = \date("Y-m-d H:i:s"); // Get current date & time. - switch ($statusCode) { - case MDConsole::STATUS_SKIPPED: - $statusSymbol = "-"; - break; - case MDConsole::STATUS_UPDATE: - $statusSymbol = "U"; - break; - case MDConsole::STATUS_ERROR: - $statusSymbol = "E"; - break; - case MDConsole::STATUS_DELETION: - $statusSymbol = "D"; - break; - case MDConsole::STATUS_NOTICE: - default: - $statusSymbol = "N"; - break; - } + $statusSymbol = match($statusCode) { + MDConsoleStatus::SKIPPED => "-", + MDConsoleStatus::UPDATE => "U", + MDConsoleStatus::ERROR => "E", + MDConsoleStatus::DELETION => "D", + MDConsoleStatus::NOTICE => "N", + }; if ($this->_context !== "") $contextStr = "{$this->_context} β”‚ "; else $contextStr = ""; @@ -166,7 +151,7 @@ final class MDOutputHandler { } } - if (($this->_verbosity > 0 && \in_array($statusCode, [MDConsole::STATUS_UPDATE, MDConsole::STATUS_DELETION, MDConsole::STATUS_ERROR], true)) || $this->_verbosity === 2) echo $message; + if (($this->_verbosity > 0 && \in_array($statusCode, [MDConsoleStatus::UPDATE, MDConsoleStatus::DELETION, MDConsoleStatus::ERROR], true)) || $this->_verbosity === 2) echo $message; if ($this->show_operation_number === true && $this->_counter % 250 === 0) { echo '―――> ' . \sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL;