Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
StartEmail
0.00% covered (danger)
0.00%
0 / 71
0.00% covered (danger)
0.00%
0 / 13
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getModuleStyles
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaderText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeaderIconName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMobileSummaryHeader
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getBody
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getMobileSummaryBody
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getEmailIcon
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getEmailAddress
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getEmailAddressRaw
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getEmailAction
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
20
 getState
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace GrowthExperiments\HomepageModules;
4
5use GrowthExperiments\ExperimentUserManager;
6use MediaWiki\Config\Config;
7use MediaWiki\Context\IContextSource;
8use MediaWiki\Html\Html;
9use MediaWiki\SpecialPage\SpecialPage;
10use OOUI\IconWidget;
11
12class StartEmail extends BaseModule {
13
14    /** @inheritDoc */
15    protected static $supportedModes = [
16        self::RENDER_DESKTOP,
17        self::RENDER_MOBILE_SUMMARY
18        // RENDER_MOBILE_DETAILS is not supported
19    ];
20
21    /** @var string */
22    protected $emailState;
23
24    /** @inheritDoc */
25    public function __construct(
26        IContextSource $context,
27        Config $wikiConfig,
28        ExperimentUserManager $experimentUserManager
29    ) {
30        parent::__construct( 'startemail', $context, $wikiConfig, $experimentUserManager );
31
32        $user = $this->getContext()->getUser();
33        if ( $user->isEmailConfirmed() ) {
34            $this->emailState = self::MODULE_STATE_CONFIRMED;
35        } elseif ( $user->getEmail() ) {
36            $this->emailState = self::MODULE_STATE_UNCONFIRMED;
37        } else {
38            $this->emailState = self::MODULE_STATE_NOEMAIL;
39        }
40    }
41
42    /** @inheritDoc */
43    protected function getModuleStyles() {
44        return array_merge(
45            parent::getModuleStyles(),
46            [ 'oojs-ui.styles.icons-alerts' ]
47        );
48    }
49
50    /** @inheritDoc */
51    protected function getHeaderText() {
52        // Not used, but must be implemented because it's abstract in the parent class
53        return '';
54    }
55
56    /** @inheritDoc */
57    protected function getHeaderIconName() {
58        // Not used, but must be implemented because it's abstract in the parent class
59        return '';
60    }
61
62    /** @inheritDoc */
63    protected function getHeader() {
64        return '';
65    }
66
67    /** @inheritDoc */
68    protected function getMobileSummaryHeader() {
69        return '';
70    }
71
72    /** @inheritDoc */
73    protected function getBody() {
74        return $this->getEmailIcon() .
75            Html::rawElement(
76                'span',
77                [ 'class' => 'growthexperiments-homepage-startemail-address-wrapper' ],
78                $this->getEmailAddress() .
79                    $this->getContext()->msg( 'word-separator' )->escaped() .
80                    $this->getEmailAction()
81            );
82    }
83
84    /** @inheritDoc */
85    protected function getMobileSummaryBody() {
86        return $this->getEmailIcon() .
87            Html::rawElement(
88                'span',
89                [ 'class' => 'growthexperiments-homepage-startemail-address-wrapper' ],
90                $this->getEmailAddressRaw() .
91                    $this->getContext()->msg( 'word-separator' )->escaped() .
92                    $this->getEmailAction()
93            );
94    }
95
96    /**
97     * Get the icon to put before the email address ('message' icon, either black or blue)
98     * @return IconWidget
99     */
100    protected function getEmailIcon() {
101        return new IconWidget( [
102            'icon' => 'message',
103            'flags' => $this->emailState === self::MODULE_STATE_NOEMAIL ? [ 'progressive' ] : []
104        ] );
105    }
106
107    /**
108     * Get the email address, if there is one, wrapped in an i18n message
109     * @return string HTML
110     */
111    protected function getEmailAddress() {
112        if ( $this->emailState === self::MODULE_STATE_NOEMAIL ) {
113            return '';
114        }
115        return $this->getContext()->msg( 'growthexperiments-homepage-email-header-startemail' )
116            ->rawParams( $this->getEmailAddressRaw() )
117            ->escaped();
118    }
119
120    /**
121     * Get the email address, if there is one, without the wrapper message
122     * @return string HTML
123     */
124    protected function getEmailAddressRaw() {
125        if ( $this->emailState === self::MODULE_STATE_NOEMAIL ) {
126            return '';
127        }
128        return Html::element(
129            'span',
130            [ 'class' => 'growthexperiments-homepage-startemail-address' ],
131            $this->getContext()->getUser()->getEmail()
132        );
133    }
134
135    /**
136     * Get the link that comes after the email address. For the NOEMAIL state, this is the only
137     * thing shown besides the icon.
138     * @return string HTML
139     */
140    protected function getEmailAction() {
141        $linkAttrs = [
142            'data-link-id' => 'email-' . $this->emailState
143        ];
144        $wrapInParentheses = false;
145        if ( $this->emailState === self::MODULE_STATE_NOEMAIL ) {
146            $label = $this->getContext()->msg( 'growthexperiments-homepage-email-button-noemail' )->text();
147            $linkAttrs['href'] = SpecialPage::getTitleFor( 'ChangeEmail' )->getLinkURL( [
148                'returnto' => $this->getContext()->getTitle()->getPrefixedText()
149            ] );
150            $linkAttrs['class'] = 'growthexperiments-homepage-startemail-noemail-link';
151        } elseif ( $this->emailState === self::MODULE_STATE_UNCONFIRMED ) {
152            $label = $this->getContext()->msg( 'growthexperiments-homepage-email-confirmlink' )->text();
153            $wrapInParentheses = true;
154            $linkAttrs['href'] = SpecialPage::getTitleFor( 'Confirmemail' )->getLinkURL();
155        } else {
156            // MODULE_STATE_CONFIRMED
157            $label = $this->getContext()->msg( 'growthexperiments-homepage-email-changelink' )->text();
158            $wrapInParentheses = true;
159            $linkAttrs['href'] = SpecialPage::getTitleFor( 'ChangeEmail' )->getLinkURL( [
160                'returnto' => $this->getContext()->getTitle()->getPrefixedText()
161            ] );
162        }
163        $link = Html::element( 'a', $linkAttrs, $label );
164        if ( $wrapInParentheses ) {
165            $link = $this->getContext()->msg( 'parentheses' )->rawParams( $link )->escaped();
166        }
167        return $link;
168    }
169
170    /** @inheritDoc */
171    public function getState() {
172        return $this->emailState;
173    }
174}