Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CaptchaStore
90.91% covered (success)
90.91%
10 / 11
66.67% covered (warning)
66.67%
2 / 3
6.03
0.00% covered (danger)
0.00%
0 / 1
 store
n/a
0 / 0
n/a
0 / 0
0
 retrieve
n/a
0 / 0
n/a
0 / 0
0
 clear
n/a
0 / 0
n/a
0 / 0
0
 cookiesNeeded
n/a
0 / 0
n/a
0 / 0
0
 get
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 unsetInstanceForTests
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\Extension\ConfirmEdit\Store;
4
5use BadMethodCallException;
6use MediaWiki\Config\ConfigException;
7use MediaWiki\MediaWikiServices;
8
9abstract class CaptchaStore {
10    /**
11     * Store the correct answer for a given captcha
12     * @param string $index
13     * @param array $info the captcha result
14     */
15    abstract public function store( $index, $info );
16
17    /**
18     * Retrieve the answer for a given captcha
19     * @param string $index
20     * @return array|false
21     */
22    abstract public function retrieve( $index );
23
24    /**
25     * Delete a result once the captcha has been used, so it cannot be reused
26     * @param string $index
27     */
28    abstract public function clear( $index );
29
30    /**
31     * Whether this type of CaptchaStore needs cookies
32     * @return bool
33     */
34    abstract public function cookiesNeeded();
35
36    /**
37     * The singleton instance
38     * @var CaptchaStore|null
39     */
40    private static $instance;
41
42    /**
43     * Get somewhere to store captcha data that will persist between requests
44     *
45     * @return CaptchaStore
46     */
47    final public static function get() {
48        if ( !self::$instance instanceof self ) {
49            $captchaStorageClass = MediaWikiServices::getInstance()->getMainConfig()
50                ->get( 'CaptchaStorageClass' );
51            if ( in_array( self::class, class_parents( $captchaStorageClass ) ) ) {
52                self::$instance = new $captchaStorageClass;
53            } else {
54                throw new ConfigException( "Invalid CaptchaStore class $captchaStorageClass" );
55            }
56        }
57        return self::$instance;
58    }
59
60    final public static function unsetInstanceForTests() {
61        if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
62            throw new BadMethodCallException( 'Cannot unset ' . __CLASS__ . ' instance in operation.' );
63        }
64        self::$instance = null;
65    }
66
67    /**
68     * Protected constructor: no creating instances except through the factory method above
69     */
70    protected function __construct() {
71    }
72}