Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
ClientHintsReferenceIds | |
100.00% |
15 / 15 |
|
100.00% |
4 / 4 |
9 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addReferenceIds | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getReferenceIds | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
mappingIdExists | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace MediaWiki\CheckUser\ClientHints; |
4 | |
5 | use LogicException; |
6 | use MediaWiki\CheckUser\Services\UserAgentClientHintsManager; |
7 | |
8 | /** |
9 | * Value object for storing reference IDs with their associated |
10 | * reference map ID. |
11 | * |
12 | * This is used, instead of a two-dimensional list, to enforce that |
13 | * the map IDs are valid. This class stores the data as a two-dimensional |
14 | * list. |
15 | */ |
16 | class ClientHintsReferenceIds { |
17 | private array $referenceIds; |
18 | |
19 | /** |
20 | * Get a new ClientHintsReferenceIds object, |
21 | * optionally setting the internal reference IDs array. |
22 | * |
23 | * @param array $referenceIds If provided, set the internal referenceIds array to |
24 | * this value. By default this is the empty array. |
25 | */ |
26 | public function __construct( array $referenceIds = [] ) { |
27 | $this->referenceIds = $referenceIds; |
28 | } |
29 | |
30 | /** |
31 | * Add reference IDs with a specific mapping ID to the internal array. |
32 | * |
33 | * @param int|int[] $referenceIds an integer or array of integers where the values are reference IDs |
34 | * @param int $mappingId any of UserAgentClientHintsManager::IDENTIFIER_* constants, which represent a valid |
35 | * map ID for the cu_useragent_clienthints_map table |
36 | * @return void |
37 | */ |
38 | public function addReferenceIds( $referenceIds, int $mappingId ): void { |
39 | if ( !$this->mappingIdExists( $mappingId ) ) { |
40 | $this->referenceIds[$mappingId] = []; |
41 | } |
42 | $referenceIds = array_map( 'intval', (array)$referenceIds ); |
43 | $this->referenceIds[$mappingId] = array_unique( array_merge( $this->referenceIds[$mappingId], $referenceIds ) ); |
44 | } |
45 | |
46 | /** |
47 | * Gets the reference IDs for a specific $mappingId, or |
48 | * if $mappingId is null, all reference IDs. |
49 | * |
50 | * @param int|null $mappingId |
51 | * @return array |
52 | */ |
53 | public function getReferenceIds( ?int $mappingId = null ): array { |
54 | if ( $mappingId === null ) { |
55 | return $this->referenceIds; |
56 | } |
57 | if ( !$this->mappingIdExists( $mappingId ) ) { |
58 | return []; |
59 | } |
60 | return $this->referenceIds[$mappingId]; |
61 | } |
62 | |
63 | /** |
64 | * Verifies that a mapping ID exists in the internal array. |
65 | * |
66 | * @param int $mappingId One of the UserAgentClientHintsManager::IDENTIFIER_* constants |
67 | * @throws LogicException if the mapping ID is not recognised |
68 | * @return bool True if the mapping ID exists in the internal array |
69 | */ |
70 | private function mappingIdExists( int $mappingId ): bool { |
71 | if ( !array_key_exists( $mappingId, UserAgentClientHintsManager::IDENTIFIER_TO_TABLE_NAME_MAP ) ) { |
72 | throw new LogicException( "Unrecognised map ID '$mappingId'" ); |
73 | } |
74 | if ( !array_key_exists( $mappingId, $this->referenceIds ) ) { |
75 | return false; |
76 | } |
77 | return true; |
78 | } |
79 | } |