Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CachedList
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getValues
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
 getCacheKey
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\Notifications;
4
5use UnexpectedValueException;
6use WANObjectCache;
7
8/**
9 * Caches an ContainmentList within WANObjectCache to prevent needing
10 * to load the nested list from a potentially slow source (mysql, etc).
11 */
12class CachedList implements ContainmentList {
13    private const ONE_WEEK = 4233600;
14
15    /** @var WANObjectCache */
16    protected $cache;
17    /** @var string */
18    protected $partialCacheKey;
19    /** @var ContainmentList */
20    protected $nestedList;
21    /** @var int */
22    protected $timeout;
23    /** @var string[]|null */
24    private $result;
25
26    /**
27     * @param WANObjectCache $cache Bag to stored cached data in.
28     * @param string $partialCacheKey Partial cache key, $nestedList->getCacheKey() will be appended
29     *   to this to construct the cache key used.
30     * @param ContainmentList $nestedList The nested EchoContainmentList to cache the result of.
31     * @param int $timeout How long in seconds to cache the nested list, defaults to 1 week.
32     */
33    public function __construct(
34        WANObjectCache $cache,
35        $partialCacheKey,
36        ContainmentList $nestedList,
37        $timeout = self::ONE_WEEK
38    ) {
39        $this->cache = $cache;
40        $this->partialCacheKey = $partialCacheKey;
41        $this->nestedList = $nestedList;
42        $this->timeout = $timeout;
43    }
44
45    /**
46     * @inheritDoc
47     */
48    public function getValues() {
49        if ( $this->result ) {
50            return $this->result;
51        }
52        $this->result = $this->cache->getWithSetCallback(
53            $this->getCacheKey(),
54            $this->timeout,
55            function () {
56                $result = $this->nestedList->getValues();
57                if ( !is_array( $result ) ) {
58                    throw new UnexpectedValueException( sprintf(
59                        "Expected array but received '%s' from '%s::getValues'",
60                        is_object( $result ) ? get_class( $result ) : gettype( $result ),
61                        get_class( $this->nestedList )
62                    ) );
63                }
64                return $result;
65            }
66        );
67        return $this->result;
68    }
69
70    /**
71     * @inheritDoc
72     */
73    public function getCacheKey() {
74        return $this->cache->makeGlobalKey(
75            'echo-containment-list',
76            $this->partialCacheKey,
77            $this->nestedList->getCacheKey()
78        );
79    }
80}