Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
CachedData | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
reset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getMaxId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getMulti | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getSize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
retrieve | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
addData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
ensureLoaded | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Flow\Import\LiquidThreadsApi; |
4 | |
5 | /** |
6 | * Abstract class to store ID-indexed cached data. |
7 | */ |
8 | abstract class CachedData { |
9 | /** @var array */ |
10 | protected $data = []; |
11 | |
12 | public function reset() { |
13 | $this->data = []; |
14 | } |
15 | |
16 | /** |
17 | * Get the value for a given ID |
18 | * |
19 | * @param int $id The ID to get |
20 | * @return mixed The data returned by retrieve() |
21 | */ |
22 | public function get( $id ) { |
23 | $result = $this->getMulti( [ $id ] ); |
24 | return reset( $result ); |
25 | } |
26 | |
27 | public function getMaxId() { |
28 | if ( $this->data ) { |
29 | return max( array_keys( $this->data ) ); |
30 | } else { |
31 | return 0; |
32 | } |
33 | } |
34 | |
35 | /** |
36 | * Get the value for a number of IDs |
37 | * |
38 | * @param int[] $ids List of IDs to retrieve |
39 | * @return array Associative array, indexed by ID. |
40 | */ |
41 | public function getMulti( array $ids ) { |
42 | $this->ensureLoaded( $ids ); |
43 | |
44 | $output = []; |
45 | foreach ( $ids as $id ) { |
46 | $output[$id] = $this->data[$id] ?? null; |
47 | } |
48 | |
49 | return $output; |
50 | } |
51 | |
52 | /** |
53 | * Gets the number of items stored in this object. |
54 | * |
55 | * @return int |
56 | */ |
57 | public function getSize() { |
58 | return count( $this->data ); |
59 | } |
60 | |
61 | /** |
62 | * Uncached retrieval of data from the backend. |
63 | * |
64 | * @param int[] $ids The IDs to retrieve data for |
65 | * @return array Associative array of data retrieved, indexed by ID. |
66 | */ |
67 | abstract protected function retrieve( array $ids ); |
68 | |
69 | /** |
70 | * Adds data to the object |
71 | * |
72 | * @param array $data Associative array, indexed by ID. |
73 | */ |
74 | protected function addData( array $data ) { |
75 | $this->data += $data; |
76 | } |
77 | |
78 | /** |
79 | * Load missing IDs from a list |
80 | * |
81 | * @param int[] $ids The IDs to retrieve |
82 | */ |
83 | protected function ensureLoaded( array $ids ) { |
84 | $missing = array_diff( $ids, array_keys( $this->data ) ); |
85 | |
86 | if ( count( $missing ) > 0 ) { |
87 | $data = $this->retrieve( $missing ); |
88 | $this->addData( $data ); |
89 | } |
90 | } |
91 | } |