From 74b50cad30afd71502599fe9d8b0b4a60245416a Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 29 Oct 2015 14:13:10 -0700 Subject: [PATCH] Move some boilerplate code into EchoEventFormatter The conversion of EchoEvent into a EchoEventPresentationModel is now done by EchoFlyoutFormatter, instead of having each subclass do it. It also does the canRender() check so subclasses don't need to worry about it. The subclasses no longer have access to the underlying EchoEvent object, so the timestamp is exposed in EchoEventPresentationModel. Change-Id: I7f0a650373eebac7aa2231b1795b51a6d031ad67 --- includes/formatters/EchoEventFormatter.php | 17 +++++++++++++++-- includes/formatters/EchoFlyoutFormatter.php | 9 ++------- includes/formatters/EventPresentationModel.php | 7 +++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/includes/formatters/EchoEventFormatter.php b/includes/formatters/EchoEventFormatter.php index d330f326d..c7aef9569 100644 --- a/includes/formatters/EchoEventFormatter.php +++ b/includes/formatters/EchoEventFormatter.php @@ -19,7 +19,20 @@ abstract class EchoEventFormatter { /** * @param EchoEvent $event - * @return string HTML + * @return string|bool Output format depends on implementation, false if it cannot be formatted */ - abstract public function format( EchoEvent $event ); + final public function format( EchoEvent $event ) { + $model = EchoEventPresentationModel::factory( $event, $this->language, $this->user ); + if ( !$model->canRender() ) { + return false; + } + + return $this->formatModel( $model ); + } + + /** + * @param EchoEventPresentationModel $model + * @return string + */ + abstract protected function formatModel( EchoEventPresentationModel $model ); } diff --git a/includes/formatters/EchoFlyoutFormatter.php b/includes/formatters/EchoFlyoutFormatter.php index 7d013ccd5..3a3b6d6a4 100644 --- a/includes/formatters/EchoFlyoutFormatter.php +++ b/includes/formatters/EchoFlyoutFormatter.php @@ -8,12 +8,7 @@ * sending HTML for backwards compatibility. */ class EchoFlyoutFormatter extends EchoEventFormatter { - public function format( EchoEvent $event ) { - $model = EchoEventPresentationModel::factory( $event, $this->language, $this->user ); - if ( !$model->canRender() ) { - return false; - } - + protected function formatModel( EchoEventPresentationModel $model ) { $icon = Html::element( 'img', array( @@ -31,7 +26,7 @@ class EchoFlyoutFormatter extends EchoEventFormatter { // @todo body text $ts = $this->language->getHumanTimestamp( - new MWTimestamp( $event->getTimestamp() ), + new MWTimestamp( $model->getTimestamp() ), null, $this->user ); diff --git a/includes/formatters/EventPresentationModel.php b/includes/formatters/EventPresentationModel.php index 95ead3331..e852eca9f 100644 --- a/includes/formatters/EventPresentationModel.php +++ b/includes/formatters/EventPresentationModel.php @@ -85,6 +85,13 @@ abstract class EchoEventPresentationModel { */ abstract public function getIconType(); + /** + * @return string Timestamp the event occurred at + */ + final public function getTimestamp() { + return $this->event->getTimestamp(); + } + /** * Helper for EchoEvent::userCan *