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
This commit is contained in:
Kunal Mehta 2015-10-29 14:13:10 -07:00 committed by Legoktm
parent a1282bcec0
commit 74b50cad30
3 changed files with 24 additions and 9 deletions

View file

@ -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 );
}

View file

@ -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
);

View file

@ -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
*