Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.16% covered (success)
92.16%
47 / 51
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Survey
92.16% covered (success)
92.16%
47 / 51
60.00% covered (warning)
60.00%
3 / 5
16.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
4.04
 getResourceLoaderModuleName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAudience
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getMessages
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
9.13
 toArray
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace QuickSurveys;
4
5abstract class Survey {
6    /**
7     * @var string The friendly name of the survey
8     */
9    private $name;
10
11    /**
12     * @var string|null The question that the survey is posing to the user
13     * @deprecated use questions array instead
14     */
15    private $question;
16
17    /**
18     * @var string|null
19     */
20    private $additionalInfo;
21
22    /**
23     * @var string|null
24     */
25    private $confirmMsg;
26
27    /**
28     * @var SurveyAudience describes the audience who can participate in a survey
29     */
30    private $audience;
31
32    /**
33     * @var string|null A user-friendly description of, or introduction to, the question
34     * @deprecated this field has been moved to SurveyQuestion
35     */
36    private $description;
37
38    /**
39     * @var float The percentage of users that will see the survey expressed as a fraction
40     */
41    private $coverage;
42
43    /**
44     * A platform can operate in one or more modes: mobile operates in 'stable' or 'beta' mode;
45     * and desktop only operates in 'stable' mode.
46     *
47     * The platforms that the survey can be displayed on, therefore, are represented as a map of
48     * platform to a set of platform modes, i.e.
49     *
50     * <code><pre>
51     * <?php
52     * $platform = array(
53     *   'desktop' => array(
54     *       'stable',
55     *   ),
56     *   'mobile' => array(
57     *       'stable',
58     *       'beta',
59     *   ),
60     * );
61     * </pre></code>
62     *
63     * @var array[] The platforms that the survey can be displayed on
64     */
65    private $platforms;
66
67    /**
68     * @var string|null The description of the privacy policy of the website that hosts the survey.
69     */
70    private $privacyPolicy;
71
72    /**
73     * @var SurveyQuestion[] The questions that the survey is posing to the user
74     */
75    private $questions;
76
77    /**
78     * @var string|null
79     */
80    private $confirmDescription;
81
82    /**
83     * @param string $name
84     * @param float $coverage
85     * @param array[] $platforms
86     * @param string|null $privacyPolicy
87     * @param string|null $additionalInfo
88     * @param string|null $confirmMsg
89     * @param SurveyAudience $audience
90     * @param string|SurveyQuestion[] $questions
91     * @param string|null $question
92     * @param string|null $description
93     * @param string|null $confirmDescription
94     */
95    public function __construct(
96        $name,
97        $coverage,
98        array $platforms,
99        $privacyPolicy,
100        $additionalInfo,
101        $confirmMsg,
102        SurveyAudience $audience,
103        $questions,
104        ?string $question = null,
105        ?string $description = null,
106        ?string $confirmDescription = null
107    ) {
108        if ( $question ) {
109            wfDeprecated( 'QuickSurveys survey with question parameter', '1.43' );
110        }
111        if ( $description ) {
112            wfDeprecated( 'QuickSurveys survey with description parameter', '1.43' );
113        }
114
115        $this->name = $name;
116        $this->question = $question;
117        $this->description = $description;
118        $this->coverage = $coverage;
119        $this->platforms = $platforms;
120        $this->privacyPolicy = $privacyPolicy;
121        $this->additionalInfo = $additionalInfo;
122        $this->confirmMsg = $confirmMsg;
123        $this->audience = $audience;
124        $this->questions = is_array( $questions ) ? $questions : [ strval( $questions ) ];
125        $this->confirmDescription = $confirmDescription;
126    }
127
128    /**
129     * Returns the name of the ResourceLoader module
130     *
131     * @return string
132     */
133    public function getResourceLoaderModuleName(): string {
134        return 'ext.quicksurveys.survey.' . str_replace( ' ', '.', $this->name );
135    }
136
137    /**
138     * @return SurveyAudience
139     */
140    public function getAudience(): SurveyAudience {
141        return $this->audience;
142    }
143
144    /**
145     * Gets the list of i18n message keys that the survey uses
146     *
147     * @return string[]
148     */
149    public function getMessages(): array {
150        $messages = [];
151
152        if ( $this->question !== null ) {
153            $messages[] = $this->question;
154        }
155
156        if ( $this->description !== null ) {
157            $messages[] = $this->description;
158        }
159
160        if ( $this->privacyPolicy !== null ) {
161            $messages[] = $this->privacyPolicy;
162        }
163
164        if ( $this->additionalInfo !== null ) {
165            $messages[] = $this->additionalInfo;
166        }
167
168        if ( $this->confirmMsg !== null ) {
169            $messages[] = $this->confirmMsg;
170        }
171
172        if ( $this->confirmDescription !== null ) {
173            $messages[] = $this->confirmDescription;
174        }
175
176        if ( $this->questions !== null ) {
177            foreach ( $this->questions as $questionItem ) {
178                $messages = array_merge( $messages, $questionItem->getMessages() );
179            }
180        }
181        return $messages;
182    }
183
184    /**
185     * Returns the JSON-encodable, minimal representation of the survey
186     *
187     * @return array
188     */
189    public function toArray(): array {
190        return [
191            'audience' => $this->audience->toArray(),
192            'name' => $this->name,
193            'question' => $this->question,
194            'description' => $this->description,
195            'module' => $this->getResourceLoaderModuleName(),
196            'coverage' => $this->coverage,
197            'platforms' => $this->platforms,
198            'privacyPolicy' => $this->privacyPolicy,
199            'additionalInfo' => $this->additionalInfo,
200            'confirmMsg' => $this->confirmMsg,
201            'questions' => array_map(
202                fn ( $question ) => $question->toArray(),
203                $this->questions
204            ),
205            'confirmDescription' => $this->confirmDescription
206        ];
207    }
208
209}