Debian Codegraph @

extensions/Echo/includes/Formatters/EchoEventFormatter.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Formatters;
4
5 use MediaWiki\Extension\Notifications\Model\Event;
6 use MediaWiki\Language\Language;
7 use MediaWiki\Logger\LoggerFactory;
8 use MediaWiki\Message\Message;
9 use MediaWiki\User\User;
10
11 /**
12 * Abstract class that each "formatter" should implement.
13 *
14 * A formatter is an output type, example formatters would be:
15 * * Special:Notifications
16 * * HTML email
17 * * plaintext email
18 *
19 * The formatter does not maintain any state except for the
20 * arguments passed in the constructor (user and language)
21 */
22 abstract class EchoEventFormatter {
23
24 /** @var User */
25 protected $user;
26
27 /** @var Language */
28 protected $language;
29
30 public function __construct( User $user, Language $language ) {
31 $this->user = $user;
32 $this->language = $language;
33 }
34
35 /**
36 * Equivalent to IContextSource::msg for the current
37 * language
38 *
39 * @param string $key
40 * @param mixed ...$params
41 * @return Message
42 */
43 protected function msg( string $key, ...$params ) {
44 $msg = wfMessage( $key, ...$params );
45 $msg->inLanguage( $this->language );
46
47 return $msg;
48 }
49
50 /**
51 * @param Event $event
52 * @param string $distributionType 'web' or 'email'
53 * @return string[]|string|false Output format depends on implementation, false if it cannot be formatted
54 */
55 final public function format( Event $event, string $distributionType = "web" ) {
56 // Deleted events should have been filtered out before getting there.
57 // This is just to be sure.
58 if ( $event->isDeleted() ) {
59 return false;
60 }
61
62 if ( !EchoEventPresentationModel::supportsPresentationModel( $event->getType() ) ) {
63 LoggerFactory::getInstance( 'Echo' )->debug(
64 "No presentation model found for event type \"{type}\"",
65 [
66 'type' => $event->getType(),
67 ]
68 );
69 return false;
70 }
71
72 $model = EchoEventPresentationModel::factory( $event, $this->language, $this->user, $distributionType );
73 if ( !$model->canRender() ) {
74 return false;
75 }
76
77 return $this->formatModel( $model );
78 }
79
80 /**
81 * @param EchoEventPresentationModel $model
82 * @return string[]|string
83 */
84 abstract protected function formatModel( EchoEventPresentationModel $model );
85 }