Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Survey
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 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
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 toArray
100.00% covered (success)
100.00%
12 / 12
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 The question that the survey is posing to the user
13     */
14    private $question;
15
16    /**
17     * @var string|null
18     */
19    private $additionalInfo;
20
21    /**
22     * @var string|null
23     */
24    private $confirmMsg;
25
26    /**
27     * @var SurveyAudience describes the audience who can participate in a survey
28     */
29    private $audience;
30
31    /**
32     * @var string|null A user-friendly description of, or introduction to, the question
33     */
34    private $description;
35
36    /**
37     * @var float The percentage of users that will see the survey expressed as a fraction
38     */
39    private $coverage;
40
41    /**
42     * A platform can operate in one or more modes: mobile operates in 'stable' or 'beta' mode;
43     * and desktop only operates in 'stable' mode.
44     *
45     * The platforms that the survey can be displayed on, therefore, are represented as a map of
46     * platform to a set of platform modes, i.e.
47     *
48     * <code><pre>
49     * <?php
50     * $platform = array(
51     *   'desktop' => array(
52     *       'stable',
53     *   ),
54     *   'mobile' => array(
55     *       'stable',
56     *       'beta',
57     *   ),
58     * );
59     * </pre></code>
60     *
61     * @var array[] The platforms that the survey can be displayed on
62     */
63    private $platforms;
64
65    /**
66     * @var string|null The description of the privacy policy of the website that hosts the survey.
67     */
68    private $privacyPolicy;
69
70    /**
71     * @param string $name
72     * @param string $question
73     * @param string|null $description
74     * @param float $coverage
75     * @param array[] $platforms
76     * @param string|null $privacyPolicy
77     * @param string|null $additionalInfo
78     * @param string|null $confirmMsg
79     * @param SurveyAudience $audience
80     */
81    public function __construct(
82        $name,
83        $question,
84        $description,
85        $coverage,
86        array $platforms,
87        $privacyPolicy,
88        $additionalInfo,
89        $confirmMsg,
90        SurveyAudience $audience
91    ) {
92        $this->name = $name;
93        $this->question = $question;
94        $this->description = $description;
95        $this->coverage = $coverage;
96        $this->platforms = $platforms;
97        $this->privacyPolicy = $privacyPolicy;
98        $this->additionalInfo = $additionalInfo;
99        $this->confirmMsg = $confirmMsg;
100        $this->audience = $audience;
101    }
102
103    /**
104     * Returns the name of the ResourceLoader module
105     *
106     * @return string
107     */
108    public function getResourceLoaderModuleName(): string {
109        return 'ext.quicksurveys.survey.' . str_replace( ' ', '.', $this->name );
110    }
111
112    /**
113     * @return SurveyAudience
114     */
115    public function getAudience(): SurveyAudience {
116        return $this->audience;
117    }
118
119    /**
120     * Gets the list of i18n message keys that the survey uses
121     *
122     * @return string[]
123     */
124    public function getMessages(): array {
125        $messages = [
126            $this->question,
127        ];
128
129        if ( $this->description !== null ) {
130            $messages[] = $this->description;
131        }
132
133        if ( $this->privacyPolicy !== null ) {
134            $messages[] = $this->privacyPolicy;
135        }
136
137        if ( $this->additionalInfo !== null ) {
138            $messages[] = $this->additionalInfo;
139        }
140
141        if ( $this->confirmMsg !== null ) {
142            $messages[] = $this->confirmMsg;
143        }
144        return $messages;
145    }
146
147    /**
148     * Returns the JSON-encodable, minimal representation of the survey
149     *
150     * @return array
151     */
152    public function toArray(): array {
153        return [
154            'audience' => $this->audience->toArray(),
155            'name' => $this->name,
156            'question' => $this->question,
157            'description' => $this->description,
158            'module' => $this->getResourceLoaderModuleName(),
159            'coverage' => $this->coverage,
160            'platforms' => $this->platforms,
161            'privacyPolicy' => $this->privacyPolicy,
162            'additionalInfo' => $this->additionalInfo,
163            'confirmMsg' => $this->confirmMsg,
164        ];
165    }
166
167}