Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RecentQuestionsFormatter
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 formatHeader
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 formatResponses
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 format
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare( strict_types = 1 );
4
5namespace GrowthExperiments\HomepageModules;
6
7use GrowthExperiments\HelpPanel\QuestionFormatter;
8use GrowthExperiments\HelpPanel\QuestionRecord;
9use MediaWiki\Context\IContextSource;
10use MediaWiki\Html\Html;
11
12class RecentQuestionsFormatter {
13
14    /**
15     * @var QuestionRecord[]
16     */
17    private array $questionRecords;
18    private IContextSource $contextSource;
19    private string $dataLinkIdKey;
20    private string $recentQuestionsCssClass;
21
22    public function __construct(
23        IContextSource $contextSource,
24        array $questionRecords,
25        string $dataLinkIdKey
26    ) {
27        $this->questionRecords = $questionRecords;
28        $this->contextSource = $contextSource;
29        $this->dataLinkIdKey = $dataLinkIdKey;
30        $this->recentQuestionsCssClass = 'recent-questions-' . $this->dataLinkIdKey;
31    }
32
33    public function formatHeader(): string {
34        return Html::element( 'h3', [], $this->contextSource
35            ->msg( 'growthexperiments-homepage-recent-questions-header' )
36            ->params( $this->contextSource->getUser() )
37            ->text()
38        );
39    }
40
41    public function formatResponses(): string {
42        $html = Html::openElement(
43            'div',
44            [ 'class' => $this->recentQuestionsCssClass . '-list' ]
45        );
46        $html .= Html::openElement( 'ul' );
47        $count = 1;
48        foreach ( $this->questionRecords as $questionRecord ) {
49            $dataLinkId = $questionRecord->isArchived() ?
50                $this->dataLinkIdKey . '-archived-' . $count :
51                $this->dataLinkIdKey . '-' . $count;
52            $questionFormatter = new QuestionFormatter(
53                $this->contextSource,
54                $questionRecord,
55                $dataLinkId,
56                'growthexperiments-homepage-recent-questions-posted-on',
57                'growthexperiments-homepage-recent-questions-archived',
58                'growthexperiments-homepage-recent-questions-archived-tooltip'
59            );
60            $html .= Html::rawElement( 'li', [], $questionFormatter->format() );
61            $count++;
62        }
63        $html .= Html::closeElement( 'ul' );
64        $html .= Html::closeElement( 'div' );
65        return $html;
66    }
67
68    public function format(): string {
69        return Html::rawElement( 'div', [ 'class' => $this->recentQuestionsCssClass ],
70            $this->questionRecords ? $this->formatHeader() . $this->formatResponses() : ''
71        );
72    }
73
74}