Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaSearchMemoryEntitiesFetcher
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Wikibase\MediaInfo\Search;
4
5class MediaSearchMemoryEntitiesFetcher extends MediaSearchEntitiesFetcher {
6    /** @var MediaSearchEntitiesFetcher */
7    protected $fetcher;
8
9    /** @var array */
10    protected static $cache = [];
11
12    public function __construct( MediaSearchEntitiesFetcher $fetcher ) {
13        $this->fetcher = $fetcher;
14    }
15
16    /**
17     * This is a wrapper around fetch, where the actual data is fetched.
18     * This simply reads from/writes to 2 intermediate caching levels:
19     * - in a property to allow for repeated calls within the same execution thread
20     * - in a separate key-value store, for follow-up requests (e.g. loading next
21     *   batch of results for the same term)
22     *
23     * @param array $searchQueries
24     * @return array
25     */
26    public function get( array $searchQueries ): array {
27        $emptyData = array_fill_keys( $searchQueries, [] );
28
29        // fetch from memory, in case it was already requested in this process
30        $fromMemory = array_intersect_key( static::$cache, $emptyData );
31
32        // fetch remaining queries from cache
33        $queriesToFetch = array_keys( array_diff_key( $emptyData, $fromMemory ) );
34        $results = $this->fetcher->get( $queriesToFetch );
35
36        // store in memory
37        static::$cache += $results;
38
39        return $fromMemory + $results + $emptyData;
40    }
41}