Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
26.67% |
4 / 15 |
|
50.00% |
4 / 8 |
CRAP | |
0.00% |
0 / 1 |
CampaignType | |
26.67% |
4 / 15 |
|
50.00% |
4 / 8 |
49.44 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getOnForAll | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getMessageKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPreferenceKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getTypes | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getById | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
ensureTypes | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | class CampaignType { |
4 | |
5 | /** @var string */ |
6 | private $id; |
7 | |
8 | /** @var bool */ |
9 | private $onForAll; |
10 | |
11 | // Prefix for creating i18n message key from id. |
12 | // Note: Coordinate with message keys (en.json and qqq.json) for types included |
13 | // as default values for $wgCentralNoticeCampaignTypes. |
14 | private const MESSAGE_KEY_PREFIX = 'centralnotice-campaign-type-'; |
15 | |
16 | // Prefix for creating preference key from id. |
17 | private const PREFERENCE_KEY_PREFIX = 'centralnotice-display-campaign-type-'; |
18 | |
19 | /** @var CampaignType[] */ |
20 | private static $types; |
21 | |
22 | /** |
23 | * @param string $id |
24 | * @param bool $onForAll |
25 | */ |
26 | public function __construct( $id, $onForAll ) { |
27 | $this->id = $id; |
28 | $this->onForAll = $onForAll; |
29 | } |
30 | |
31 | /** |
32 | * @return string |
33 | */ |
34 | public function getId() { |
35 | return $this->id; |
36 | } |
37 | |
38 | /** |
39 | * @return bool |
40 | */ |
41 | public function getOnForAll() { |
42 | return $this->onForAll; |
43 | } |
44 | |
45 | /** |
46 | * @return string |
47 | */ |
48 | public function getMessageKey() { |
49 | // The following messages are generated here: |
50 | // * centralnotice-campaign-type-advocacy |
51 | // * centralnotice-campaign-type-article-writing |
52 | // * centralnotice-campaign-type-photography |
53 | // * centralnotice-campaign-type-event |
54 | // * centralnotice-campaign-type-fundraising |
55 | // * centralnotice-campaign-type-governance |
56 | // * centralnotice-campaign-type-maintenance |
57 | // * centralnotice-campaign-type-special |
58 | return self::MESSAGE_KEY_PREFIX . $this->id; |
59 | } |
60 | |
61 | /** |
62 | * @return string |
63 | */ |
64 | public function getPreferenceKey() { |
65 | // The following messages are generated here: |
66 | // * centralnotice-display-campaign-type-advocacy |
67 | // * centralnotice-display-campaign-type-article-writing |
68 | // * centralnotice-display-campaign-type-photography |
69 | // * centralnotice-display-campaign-type-event |
70 | // * centralnotice-display-campaign-type-fundraising |
71 | // * centralnotice-display-campaign-type-governance |
72 | // * centralnotice-display-campaign-type-maintenance |
73 | // * centralnotice-display-campaign-type-special |
74 | return self::PREFERENCE_KEY_PREFIX . $this->id; |
75 | } |
76 | |
77 | /** |
78 | * Get all available campaign types |
79 | * |
80 | * @return CampaignType[] |
81 | */ |
82 | public static function getTypes() { |
83 | self::ensureTypes(); |
84 | return array_values( self::$types ); |
85 | } |
86 | |
87 | /** |
88 | * Get a campaign type by id |
89 | * |
90 | * @param string $id |
91 | * @return CampaignType|null Campaign type requested, or null if it doesn't exist |
92 | */ |
93 | public static function getById( string $id ) { |
94 | self::ensureTypes(); |
95 | // This needs to be robust in case configuration changes and removes types that |
96 | // remain in the DB. |
97 | return self::$types[ $id ] ?? null; |
98 | } |
99 | |
100 | private static function ensureTypes() { |
101 | global $wgCentralNoticeCampaignTypes; |
102 | |
103 | if ( !self::$types ) { |
104 | self::$types = []; |
105 | foreach ( $wgCentralNoticeCampaignTypes as $id => $props ) { |
106 | self::$types[ $id ] = |
107 | new CampaignType( $id, $props[ "onForAll" ] ); |
108 | } |
109 | } |
110 | } |
111 | } |