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