Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CachedSuggestionsInfo
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getInfo
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace GrowthExperiments\NewcomerTasks;
4
5use WANObjectCache;
6
7/**
8 * A CachedSuggestionsInfo decorator which uses WANObjectCache to get/set information about tasks
9 */
10class CachedSuggestionsInfo implements NewcomerTasksInfo {
11
12    /**
13     * @var SuggestionsInfo
14     */
15    private $suggestionsInfo;
16
17    /**
18     * @var WANObjectCache
19     */
20    private $cache;
21
22    /**
23     * @param SuggestionsInfo $suggestionsInfo
24     * @param WANObjectCache $cache
25     */
26    public function __construct(
27        SuggestionsInfo $suggestionsInfo,
28        WANObjectCache $cache
29    ) {
30        $this->suggestionsInfo = $suggestionsInfo;
31        $this->cache = $cache;
32    }
33
34    /** @inheritDoc */
35    public function getInfo( array $options = [] ) {
36        // Force the value to be regenerated if the cached value should not be used
37        $resetCache = $options[ 'resetCache' ] ?? false;
38        $cacheOption = $resetCache ? [ 'minAsOf' => INF ] : [];
39        return $this->cache->getWithSetCallback(
40            $this->cache->makeKey( 'GrowthExperiments', 'SuggestionsInfo' ),
41            $this->cache::TTL_HOUR,
42            function ( $oldValue, &$ttl, &$setOpts ) {
43                $data = $this->suggestionsInfo->getInfo();
44                if ( !$data || isset( $data['error'] ) ) {
45                    $ttl = $this->cache::TTL_UNCACHEABLE;
46                } else {
47                    // WANObjectCache::fetchOrRegenerate would set this to the start of callback
48                    // execution if unset. If at the end of the callback more than a few seconds
49                    // have passed since the given time, it will refuse to cache.
50                    $setOpts['since'] = INF;
51                }
52                return $data;
53            },
54            $cacheOption
55        );
56    }
57
58}