diff --git a/src/Services/LogSystem/LogEntryExtraFormatter.php b/src/Services/LogSystem/LogEntryExtraFormatter.php index bf386373..d251405d 100644 --- a/src/Services/LogSystem/LogEntryExtraFormatter.php +++ b/src/Services/LogSystem/LogEntryExtraFormatter.php @@ -42,6 +42,8 @@ declare(strict_types=1); namespace App\Services\LogSystem; +use App\Entity\Contracts\LogWithCommentInterface; +use App\Entity\Contracts\LogWithEventUndoInterface; use App\Entity\LogSystem\AbstractLogEntry; use App\Entity\LogSystem\CollectionElementDeleted; use App\Entity\LogSystem\DatabaseUpdatedLogEntry; @@ -65,6 +67,9 @@ class LogEntryExtraFormatter protected $translator; protected $elementTypeNameGenerator; + protected const CONSOLE_SEARCH = ['', '', '', '', ' ']; + protected const CONSOLE_REPLACE = ['', '', '', '', '→']; + public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator) { $this->translator = $translator; @@ -78,32 +83,33 @@ class LogEntryExtraFormatter */ public function formatConsole(AbstractLogEntry $logEntry): string { - $tmp = $this->format($logEntry); + $arr = $this->getInternalFormat($logEntry); + $tmp = []; - //Just a simple tweak to make the console output more pretty. - $search = ['', '', '', '', ' ']; - $replace = ['', '', '', '', '→']; + //Make an array with entries in the form "Key: Value" + foreach ($arr as $key => $value) { + $str = ''; + if (is_string($key)) { + $str .= '' . $this->translator->trans($key) . ': '; + } + $str .= $value; + if (!empty($str)) { + $tmp[] = $str; + } + } - return str_replace($search, $replace, $tmp); + return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode("; ", $tmp)); } - /** - * Return a HTML formatted string containing a user viewable form of the Extra data. - * - * @return string - */ - public function format(AbstractLogEntry $context): string + protected function getInternalFormat(AbstractLogEntry $context): array { + $array = []; if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) { - return sprintf( - '%s: %s', - $this->translator->trans('log.user_login.ip'), - htmlspecialchars($context->getIPAddress()) - ); + $array['log.user_login.ip'] = htmlspecialchars($context->getIPAddress()); } if ($context instanceof ExceptionLogEntry) { - return sprintf( + $array[] = sprintf( '%s %s:%d : %s', htmlspecialchars($context->getExceptionClass()), htmlspecialchars($context->getFile()), @@ -113,7 +119,7 @@ class LogEntryExtraFormatter } if ($context instanceof DatabaseUpdatedLogEntry) { - return sprintf( + $array[] = sprintf( '%s %s %s', $this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'), $context->getOldVersion(), @@ -121,97 +127,50 @@ class LogEntryExtraFormatter ); } - if ($context instanceof ElementCreatedLogEntry ) { - $comment = ''; + if ($context instanceof LogWithEventUndoInterface) { if ($context->isUndoEvent()) { if ($context->getUndoMode() === 'undo') { - $comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; - } elseif($context->getUndoMode() === 'revert') { - $comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';'; + $array['log.undo_mode.undo'] = (string) $context->getUndoEventID(); + } elseif ($context->getUndoMode() === 'revert') { + $array['log.undo_mode.revert'] = (string) $context->getUndoEventID(); } } - if ($context->hasComment()) { - $comment .= htmlspecialchars($context->getComment()) . '; '; - } - if($context->hasCreationInstockValue()) { - return $comment . sprintf( - '%s: %s', - $this->translator->trans('log.element_created.original_instock'), - $context->getCreationInstockValue() - ); - } - return $comment; + } + + if ($context instanceof LogWithCommentInterface && $context->hasComment()) { + $array[] = htmlspecialchars($context->getComment()); + } + + if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) { + $array['log.element_created.original_instock'] = (string) $context->getCreationInstockValue(); } if ($context instanceof ElementDeletedLogEntry) { - $comment = ''; - if ($context->isUndoEvent()) { - if ($context->getUndoMode() === 'undo') { - $comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; - } elseif($context->getUndoMode() === 'revert') { - $comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';'; - } + if ($context->getOldName() !== null) { + $array['log.element_deleted.old_name'] = htmlspecialchars($context->getOldName()); + } else { + $array['log.element_deleted.old_name'] = $this->translator->trans('log.element_deleted.old_name.unknown'); } - if ($context->hasComment()) { - $comment .= htmlspecialchars($context->getComment()) . '; '; - } - return $comment . sprintf( - '%s: %s', - $this->translator->trans('log.element_deleted.old_name'), - $context->getOldName() ?? $this->translator->trans('log.element_deleted.old_name.unknown') - ); } - if ($context instanceof ElementEditedLogEntry) { - $str = ''; - if ($context->isUndoEvent()) { - if ($context->getUndoMode() === 'undo') { - $str .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; - } elseif($context->getUndoMode() === 'revert') { - $str .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';'; - } - } - if ($context->hasComment()) { - $str .= htmlspecialchars($context->getComment()); - } - - if ($context->hasChangedFieldsInfo()) { - if (!empty($str)) { - $str .= '; '; - } - $str .= sprintf( - "%s: %s", - $this->translator->trans('log.element_edited.changed_fields'), - htmlspecialchars(implode(', ', $context->getChangedFields())) - ); - } - return $str; + if ($context instanceof ElementEditedLogEntry && $context->hasChangedFieldsInfo()) { + $array['log.element_edited.changed_fields'] = htmlspecialchars(implode(', ', $context->getChangedFields())); } if ($context instanceof InstockChangedLogEntry) { - return sprintf( - '%s; %s %s (%s); %s: %s', - $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'), + $array[] = $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'); + $array[] = sprintf( + '%s %s (%s)', $context->getOldInstock(), $context->getNewInstock(), - (! $context->isWithdrawal() ? '+' : '-').$context->getDifference(true), - $this->translator->trans('log.instock_changed.comment'), - htmlspecialchars($context->getComment()) + (! $context->isWithdrawal() ? '+' : '-').$context->getDifference(true) ); + $array['log.instock_changed.comment'] = htmlspecialchars($context->getComment()); } if ($context instanceof CollectionElementDeleted) { - $comment = ''; - if ($context->isUndoEvent()) { - if ($context->getUndoMode() === 'undo') { - $comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; - } elseif($context->getUndoMode() === 'revert') { - $comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';'; - } - } - - return $comment . sprintf('%s: %s: %s (%s)', - $this->translator->trans('log.collection_deleted.deleted'), + $array['log.collection_deleted.deleted'] = sprintf( + '%s: %s (%s)', $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getDeletedElementClass()), $context->getOldName() ?? $context->getDeletedElementID(), $context->getCollectionName() @@ -219,9 +178,34 @@ class LogEntryExtraFormatter } if ($context instanceof UserNotAllowedLogEntry) { - return htmlspecialchars($context->getMessage()); + $array[] = htmlspecialchars($context->getMessage()); } - return ''; + return $array; + } + + /** + * Return a HTML formatted string containing a user viewable form of the Extra data. + * + * @return string + */ + public function format(AbstractLogEntry $context): string + { + $arr = $this->getInternalFormat($context); + $tmp = []; + + //Make an array with entries in the form "Key: Value" + foreach ($arr as $key => $value) { + $str = ''; + if (is_string($key)) { + $str .= '' . $this->translator->trans($key) . ': '; + } + $str .= $value; + if (!empty($str)) { + $tmp[] = $str; + } + } + + return implode("; ", $tmp); } }