Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuestionFormatter
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 format
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 formatUnarchived
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 formatArchived
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 getPostedOnHtml
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getRelativeTime
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace GrowthExperiments\HelpPanel;
6
7use GrowthExperiments\Util;
8use MediaWiki\Context\IContextSource;
9use MediaWiki\Html\Html;
10
11class QuestionFormatter {
12
13    private IContextSource $contextSource;
14    private string $dataLinkId;
15    private string $postedOnMsgKey;
16    private string $archivedMsgKey;
17    private string $archivedTooltipMsgKey;
18    private QuestionRecord $questionRecord;
19
20    public function __construct(
21        IContextSource $contextSource,
22        QuestionRecord $questionRecord,
23        string $dataLinkId,
24        string $postedOnMsgKey,
25        string $archivedMsgKey,
26        string $archivedTooltipMsgKey
27    ) {
28        $this->contextSource = $contextSource;
29        $this->dataLinkId = $dataLinkId;
30        $this->postedOnMsgKey = $postedOnMsgKey;
31        $this->archivedMsgKey = $archivedMsgKey;
32        $this->questionRecord = $questionRecord;
33        $this->archivedTooltipMsgKey = $archivedTooltipMsgKey;
34    }
35
36    public function format(): string {
37        return $this->questionRecord->isArchived() ? $this->formatArchived() : $this->formatUnarchived();
38    }
39
40    private function formatUnarchived(): string {
41        return Html::rawElement(
42            'div',
43            [ 'class' => 'question-link-wrapper' ],
44            Html::element(
45                'a',
46                [
47                    'class' => 'question-text',
48                    'href' => $this->questionRecord->getResultUrl(),
49                    'data-link-id' => $this->dataLinkId
50                ],
51                $this->questionRecord->getQuestionText()
52            )
53        ) .    $this->getPostedOnHtml();
54    }
55
56    private function formatArchived(): string {
57        return Html::rawElement(
58                'div',
59                [ 'class' => 'question-link-wrapper question-archived' ],
60                Html::element(
61                        'span',
62                        [ 'class' => 'question-text' ],
63                        $this->questionRecord->getQuestionText()
64                    ) .
65                    ' (' .
66                    Html::element(
67                        'a',
68                        [
69                            'href' => $this->questionRecord->getArchiveUrl(),
70                            'data-link-id' => $this->dataLinkId,
71                            'title' => $this->contextSource->msg( $this->archivedTooltipMsgKey )->text()
72                        ],
73                        $this->contextSource->msg( $this->archivedMsgKey )->text()
74                    ) .
75                    ')'
76            ) .
77            $this->getPostedOnHtml();
78    }
79
80    private function getPostedOnHtml(): string {
81        return Html::element(
82                'span',
83                [ 'class' => 'question-posted-on' ],
84                $this->contextSource
85                    ->msg( $this->postedOnMsgKey )
86                    ->params( $this->getRelativeTime() )
87                    ->text()
88        );
89    }
90
91    private function getRelativeTime(): string {
92        $elapsedTime = (int)wfTimestamp() - (int)wfTimestamp(
93            TS_UNIX,
94            $this->questionRecord->getTimestamp()
95        );
96        return Util::getRelativeTime( $this->contextSource, $elapsedTime );
97    }
98}