Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoPlainTextDigestEmailFormatter
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 formatModels
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
20
 getCategoryTitle
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications\Formatters;
4
5use Language;
6use MediaWiki\Parser\Sanitizer;
7use MediaWiki\SpecialPage\SpecialPage;
8use MediaWiki\User\User;
9
10class EchoPlainTextDigestEmailFormatter extends EchoEventDigestFormatter {
11
12    /**
13     * @var string 'daily' or 'weekly'
14     */
15    protected $digestMode;
16
17    public function __construct( User $user, Language $language, $digestMode ) {
18        parent::__construct( $user, $language );
19        $this->digestMode = $digestMode;
20    }
21
22    /**
23     * @param EchoEventPresentationModel[] $models
24     * @return string[] Array of the following format:
25     *               [ 'body'    => formatted email body,
26     *                 'subject' => formatted email subject ]
27     */
28    protected function formatModels( array $models ) {
29        $content = [];
30        foreach ( $models as $model ) {
31            $content[$model->getCategory()][] = Sanitizer::stripAllTags( $model->getHeaderMessage()->parse() );
32        }
33
34        ksort( $content );
35
36        // echo-email-batch-body-intro-daily
37        // echo-email-batch-body-intro-weekly
38        $text = $this->msg( 'echo-email-batch-body-intro-' . $this->digestMode )
39            ->params( $this->user->getName() )->text();
40
41        // Does this need to be a message?
42        $bullet = $this->msg( 'echo-email-batch-bullet' )->text();
43
44        foreach ( $content as $type => $items ) {
45            $text .= "\n\n--\n\n";
46            $text .= $this->getCategoryTitle( $type, count( $items ) );
47            $text .= "\n";
48            foreach ( $items as $item ) {
49                $text .= "\n$bullet $item";
50            }
51        }
52
53        $colon = $this->msg( 'colon-separator' )->text();
54        $text .= "\n\n--\n\n";
55        $viewAll = $this->msg( 'echo-email-batch-link-text-view-all-notifications' )->text();
56        $link = SpecialPage::getTitleFor( 'Notifications' )->getFullURL( '', false, PROTO_CANONICAL );
57        $text .= "$viewAll$colon <$link>";
58
59        $plainTextFormatter = new EchoPlainTextEmailFormatter( $this->user, $this->language );
60
61        $text .= "\n\n{$plainTextFormatter->getFooter()}";
62
63        // echo-email-batch-subject-daily
64        // echo-email-batch-subject-weekly
65        $subject = $this->msg( 'echo-email-batch-subject-' . $this->digestMode )
66            ->numParams( count( $models ), count( $models ) )
67            ->text();
68
69        return [
70            'subject' => $subject,
71            'body' => $text,
72        ];
73    }
74
75    /**
76     * @param string $type Notification type
77     * @param int $count Number of notifications in this type's section
78     * @return string Formatted category section title
79     */
80    private function getCategoryTitle( $type, $count ) {
81        return $this->msg( "echo-category-title-$type" )
82            ->numParams( $count )
83            ->text();
84    }
85}