Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
91.67% |
11 / 12 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
StatsCache | |
91.67% |
11 / 12 |
|
80.00% |
4 / 5 |
8.04 | |
0.00% |
0 / 1 |
get | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllMetrics | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
cacheKey | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | declare( strict_types=1 ); |
22 | |
23 | namespace Wikimedia\Stats; |
24 | |
25 | use TypeError; |
26 | use Wikimedia\Stats\Metrics\MetricInterface; |
27 | use Wikimedia\Stats\Metrics\NullMetric; |
28 | |
29 | /** |
30 | * Singleton cache for Metric instances. |
31 | * |
32 | * For reuse and collision avoidance. Serves as the data source for Metric Emitters. |
33 | * |
34 | * @author Cole White |
35 | * @since 1.41 |
36 | */ |
37 | class StatsCache { |
38 | /** @var string */ |
39 | public const DELIMITER = '.'; |
40 | |
41 | /** @var MetricInterface[] */ |
42 | private array $cache = []; |
43 | |
44 | /** |
45 | * Get a metric object from cache or null. |
46 | * |
47 | * @param string $component |
48 | * @param string $name |
49 | * @param string $expectedClass |
50 | * @return MetricInterface|null |
51 | * @throws TypeError If cached value is for a different metric type. |
52 | */ |
53 | public function get( string $component, string $name, string $expectedClass ) { |
54 | $key = self::cacheKey( $component, $name ); |
55 | $metric = $this->cache[$key] ?? null; |
56 | if ( $metric !== null && get_class( $metric ) !== $expectedClass ) { |
57 | throw new TypeError( "Encountered metric name collision: $key defined as " |
58 | . get_class( $metric ) . " but $expectedClass was requested" ); |
59 | } |
60 | return $metric; |
61 | } |
62 | |
63 | /** |
64 | * Add a metric object to the cache. |
65 | * |
66 | * @param string $component |
67 | * @param string $name |
68 | * @param MetricInterface|NullMetric $metric |
69 | */ |
70 | public function set( string $component, string $name, $metric ): void { |
71 | $this->cache[self::cacheKey( $component, $name )] = $metric; |
72 | } |
73 | |
74 | /** |
75 | * Get all metrics from cache. |
76 | * |
77 | * @return MetricInterface[] |
78 | */ |
79 | public function getAllMetrics(): array { |
80 | return $this->cache; |
81 | } |
82 | |
83 | /** |
84 | * Clears the cache. |
85 | * |
86 | * @return void |
87 | */ |
88 | public function clear(): void { |
89 | $this->cache = []; |
90 | } |
91 | |
92 | /** |
93 | * Get the metric formatted name. |
94 | * |
95 | * Takes the provided name and constructs a more specific name by combining |
96 | * component and name. |
97 | * |
98 | * @param string $component |
99 | * @param string $name |
100 | * @return string |
101 | */ |
102 | private static function cacheKey( string $component, string $name ): string { |
103 | // mitigate collision of empty-component metric with a component metric |
104 | if ( $component !== '' ) { |
105 | return implode( self::DELIMITER, [ $component, $name ] ); |
106 | } |
107 | return $name; |
108 | } |
109 | } |