Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
ArrayStatsStore | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
11 | |
100.00% |
1 / 1 |
makeKey | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
incr | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
delete | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
query | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikimedia\WRStats; |
4 | |
5 | /** |
6 | * In-memory stats store. |
7 | */ |
8 | class ArrayStatsStore implements StatsStore { |
9 | /** |
10 | * @var array[] Associative array mapping data keys to arrays where the |
11 | * first entry is the value and the second is the TTL. |
12 | */ |
13 | private $data = []; |
14 | |
15 | public function makeKey( $prefix, $internals, $entity ) { |
16 | $globality = $entity->isGlobal() ? 'global' : 'local'; |
17 | return implode( ':', |
18 | array_merge( [ $globality ], $prefix, $internals, $entity->getComponents() ) |
19 | ); |
20 | } |
21 | |
22 | public function incr( array $values, $ttl ) { |
23 | foreach ( $values as $key => $value ) { |
24 | if ( !isset( $this->data[$key] ) ) { |
25 | $this->data[$key] = [ 0, $ttl ]; |
26 | } |
27 | $this->data[$key][0] += $value; |
28 | } |
29 | } |
30 | |
31 | public function delete( array $keys ) { |
32 | foreach ( $keys as $key ) { |
33 | unset( $this->data[$key] ); |
34 | } |
35 | } |
36 | |
37 | public function query( array $keys ) { |
38 | $values = []; |
39 | foreach ( $keys as $key ) { |
40 | if ( isset( $this->data[$key] ) ) { |
41 | $values[$key] = $this->data[$key][0]; |
42 | } |
43 | } |
44 | return $values; |
45 | } |
46 | |
47 | /** |
48 | * @return array |
49 | */ |
50 | public function getData() { |
51 | return $this->data; |
52 | } |
53 | } |