Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
QuestionPosterFactory
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 getQuestionPoster
0.00% covered (danger)
0.00%
0 / 45
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace GrowthExperiments\HelpPanel\QuestionPoster;
4
5use GrowthExperiments\Mentorship\MentorManager;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Page\WikiPageFactory;
8use MediaWiki\Permissions\PermissionManager;
9use MediaWiki\Title\TitleFactory;
10use UserNotLoggedIn;
11use Wikimedia\Assert\Assert;
12use Wikimedia\Stats\StatsFactory;
13
14/**
15 * Factory class for selecting the right question poster based on where the questions should go
16 * and where they are sent from.
17 */
18class QuestionPosterFactory {
19
20    /** The question is sent from the Mentorship module of the home page. */
21    public const SOURCE_MENTORSHIP_MODULE = 'mentorship module';
22    /** The question is sent from the help panel. */
23    public const SOURCE_HELP_PANEL = 'help panel';
24
25    /** The question is sent to the wiki's helpdesk. */
26    public const TARGET_HELPDESK = 'helpdesk';
27    /** The question is sent to the talk page of the asking user's mentor. */
28    public const TARGET_MENTOR_TALK = 'mentor talk page';
29
30    /** @var WikiPageFactory */
31    private $wikiPageFactory;
32
33    /** @var TitleFactory */
34    private $titleFactory;
35
36    /** @var MentorManager */
37    private $mentorManager;
38
39    /** @var PermissionManager */
40    private $permissionManager;
41
42    /** @var bool */
43    private $helpDeskPostOnTop;
44
45    /** @var StatsFactory */
46    private $statsFactory;
47
48    private bool $confirmEditInstalled;
49    private bool $flowInstalled;
50
51    /**
52     * @param WikiPageFactory $wikiPageFactory
53     * @param TitleFactory $titleFactory
54     * @param MentorManager $mentorManager
55     * @param PermissionManager $permissionManager
56     * @param bool $helpDeskPostOnTop Whether to post on top of the help desk
57     *   (as opposed to the bottom). Only affects wikitext pages.
58     * @param statsFactory $statsFactory
59     * @param bool $confirmEditInstalled
60     * @param bool $flowInstalled
61     */
62    public function __construct(
63        WikiPageFactory $wikiPageFactory,
64        TitleFactory $titleFactory,
65        MentorManager $mentorManager,
66        PermissionManager $permissionManager,
67        bool $helpDeskPostOnTop,
68        StatsFactory $statsFactory,
69        bool $confirmEditInstalled,
70        bool $flowInstalled
71    ) {
72        $this->wikiPageFactory = $wikiPageFactory;
73        $this->titleFactory = $titleFactory;
74        $this->mentorManager = $mentorManager;
75        $this->permissionManager = $permissionManager;
76        $this->helpDeskPostOnTop = $helpDeskPostOnTop;
77        $this->statsFactory = $statsFactory;
78        $this->confirmEditInstalled = $confirmEditInstalled;
79        $this->flowInstalled = $flowInstalled;
80    }
81
82    /**
83     * @param string $source One of the SOURCE_* constants.
84     * @param string $target One of the TARGET_* constants.
85     * @param IContextSource $context
86     * @param string $body Wikitext of the question.
87     * @param string $relevantTitle Title of the page the question is about, if any.
88     * @return QuestionPoster
89     * @throws UserNotLoggedIn
90     */
91    public function getQuestionPoster(
92        string $source,
93        string $target,
94        IContextSource $context,
95        string $body,
96        string $relevantTitle = ''
97    ): QuestionPoster {
98        Assert::parameter(
99            in_array( $source, [ self::SOURCE_MENTORSHIP_MODULE, self::SOURCE_HELP_PANEL ], true ),
100            '$source', 'must be one of the QuestionPosterFactory::SOURCE_* constants' );
101        Assert::parameter(
102            in_array( $target, [ self::TARGET_HELPDESK, self::TARGET_MENTOR_TALK ], true ),
103            '$target', 'must be one of the QuestionPosterFactory::TARGET_* constants' );
104
105        if ( $target === self::TARGET_HELPDESK ) {
106            $questionPoster = new HelpdeskQuestionPoster(
107                $this->wikiPageFactory,
108                $this->titleFactory,
109                $this->permissionManager,
110                $this->statsFactory,
111                $this->confirmEditInstalled,
112                $this->flowInstalled,
113                $context,
114                $body,
115                $relevantTitle
116            );
117            $questionPoster->setPostOnTop( $this->helpDeskPostOnTop );
118            return $questionPoster;
119        } elseif ( $source === self::SOURCE_HELP_PANEL ) {
120            return new HelppanelMentorQuestionPoster(
121                $this->wikiPageFactory,
122                $this->titleFactory,
123                $this->mentorManager,
124                $this->permissionManager,
125                $this->statsFactory,
126                $this->confirmEditInstalled,
127                $this->flowInstalled,
128                $context,
129                $body,
130                $relevantTitle
131            );
132        } else {
133            return new HomepageMentorQuestionPoster(
134                $this->wikiPageFactory,
135                $this->titleFactory,
136                $this->mentorManager,
137                $this->permissionManager,
138                $this->statsFactory,
139                $this->confirmEditInstalled,
140                $this->flowInstalled,
141                $context,
142                $body,
143                $relevantTitle
144            );
145        }
146    }
147
148}