Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
5 / 8
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
BlockCache
62.50% covered (warning)
62.50%
5 / 8
66.67% covered (warning)
66.67%
2 / 3
9.58
0.00% covered (danger)
0.00%
0 / 1
 get
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearUser
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Block;
4
5use MediaWiki\User\UserIdentity;
6
7/**
8 * @internal For use by BlockManager
9 */
10class BlockCache {
11    /** @var BlockCacheEntry[] The cache entries indexed by partial key */
12    private $cache = [];
13
14    /**
15     * Get a cached Block for the given key, or null if there is no cache entry,
16     * or false if there is a cache entry indicating that the given target is
17     * not blocked.
18     *
19     * @param BlockCacheKey $key
20     * @return AbstractBlock|false|null
21     */
22    public function get( BlockCacheKey $key ) {
23        $entry = $this->cache[$key->getPartialKey()] ?? null;
24        if ( $entry === null ) {
25            return null;
26        }
27        return $key->matchesStored( $entry->key ) ? $entry->block : null;
28    }
29
30    /**
31     * Set a block cache entry
32     *
33     * @param BlockCacheKey $key The bundled block cache parameters
34     * @param AbstractBlock|false $value The block, or false to indicate that
35     *   the target is not blocked.
36     */
37    public function set( BlockCacheKey $key, $value ) {
38        $this->cache[$key->getPartialKey()] = new BlockCacheEntry( $key, $value );
39    }
40
41    /**
42     * Clear all block cache entries associated with a user
43     *
44     * @param UserIdentity $user
45     */
46    public function clearUser( UserIdentity $user ) {
47        foreach ( $this->cache as $partialKey => $entry ) {
48            if ( $entry->key->isUser( $user ) ) {
49                unset( $this->cache[$partialKey] );
50            }
51        }
52    }
53}