Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CacheRepository
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 factory
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheKeyType
n/a
0 / 0
n/a
0 / 0
0
 getCacheKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 delete
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 has
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OAuth\Repository;
4
5use BagOStuff;
6use MediaWiki\Extension\OAuth\Backend\Utils;
7
8abstract class CacheRepository {
9
10    /**
11     * @var BagOStuff
12     */
13    protected $cache;
14
15    /**
16     * @return static
17     */
18    public static function factory() {
19        $cache = Utils::getSessionCache();
20
21        // @phan-suppress-next-line PhanTypeInstantiateAbstractStatic
22        return new static( $cache );
23    }
24
25    /**
26     * @param BagOStuff $cache
27     */
28    protected function __construct( BagOStuff $cache ) {
29        $this->cache = $cache;
30    }
31
32    /**
33     * Get object type for session key
34     *
35     * @return string
36     */
37    abstract protected function getCacheKeyType(): string;
38
39    /**
40     * Get the cache key based on unique identifier
41     *
42     * @param string $id
43     * @return string
44     */
45    protected function getCacheKey( $id ) {
46        return Utils::getCacheKey( $this->getCacheKeyType(), $id );
47    }
48
49    /**
50     * @param string $identifier
51     * @param int $flags
52     * @return mixed
53     */
54    protected function get( $identifier, $flags = 0 ) {
55        return $this->cache->get( $this->getCacheKey( $identifier ), $flags );
56    }
57
58    /**
59     * @param string $identifier
60     * @param mixed $value
61     * @param int $expires
62     * @param int $flags
63     */
64    protected function set( $identifier, $value, $expires = 0, $flags = 0 ) {
65        $this->cache->add( $this->getCacheKey( $identifier ), $value, $expires, $flags );
66    }
67
68    /**
69     * @param string $identifier
70     * @param int $flags
71     */
72    protected function delete( $identifier, $flags = 0 ) {
73        $this->cache->delete( $this->getCacheKey( $identifier ), $flags );
74    }
75
76    /**
77     * Convenience method to determine if given key exists in cache
78     *
79     * @param string $identifier
80     * @return bool
81     */
82    protected function has( $identifier ) {
83        return $this->cache->get( $this->getCacheKey( $identifier ) ) !== false;
84    }
85}