Passed
Push — master ( 2d98cc...0986c5 )
by Daimona
01:45
created

Config::getWikiMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Exception\ConfigException;
6
use BotRiconferme\Exception\MissingPageException;
7
8
/**
9
 * Singleton class holding user-defined config
10
 */
11
class Config {
12
	/** @var self */
13
	private static $instance;
14
	/** @var array */
15
	private $opts = [];
16
17
	/**
18
	 * Use self::init() and self::getInstance()
19
	 */
20
	private function __construct() {
21
	}
22
23
	/**
24
	 * Initialize a new self instance with CLI params set and retrieve on-wiki config.
25
	 *
26
	 * @param array $defaults
27
	 * @throws ConfigException
28
	 */
29
	public static function init( array $defaults ) {
30
		if ( self::$instance ) {
31
			throw new ConfigException( 'Config was already initialized' );
32
		}
33
34
		$inst = new self;
35
		$inst->set( 'list-title', $defaults['list-title'] );
36
		$inst->set( 'msg-title', $defaults['msg-title'] );
37
		$inst->set( 'username', $defaults['username'] );
38
		$inst->set( 'password', $defaults['password'] );
39
		self::$instance = $inst;
40
41
		// On-wiki values
42
		try {
43
			$conf = ( new WikiController )->getPageContent( $defaults[ 'config-title' ] );
44
		} catch ( MissingPageException $e ) {
45
			throw new ConfigException( 'Please create a config page.' );
46
		}
47
48
		foreach ( json_decode( $conf, true ) as $key => $val ) {
49
			self::$instance->set( $key, $val );
50
		}
51
	}
52
53
	/**
54
	 * @param string $key
55
	 * @return string
56
	 */
57
	public function getWikiMessage( string $key ) : string {
58
		static $messages = null;
59
		if ( $messages === null ) {
60
			try {
61
				$messages = ( new WikiController )->getPageContent( $this->opts[ 'msg-title' ] );
62
			} catch ( MissingPageException $e ) {
63
				throw new ConfigException( 'Please create a messages page.' );
64
			}
65
		}
66
		return $messages[$key];
67
	}
68
	/**
69
	 * Set a config value.
70
	 *
71
	 * @param string $key
72
	 * @param mixed $value
73
	 */
74
	protected function set( string $key, $value ) {
75
		$this->opts[ $key ] = $value;
76
	}
77
78
	/**
79
	 * Generic instance getter
80
	 *
81
	 * @return self
82
	 * @throws ConfigException
83
	 */
84
	public static function getInstance() : self {
85
		if ( !self::$instance ) {
86
			throw new ConfigException( 'Config not yet initialized' );
87
		}
88
		return self::$instance;
89
	}
90
91
	/**
92
	 * Get the requested option, or fail if it doesn't exist
93
	 *
94
	 * @param string $opt
95
	 * @return mixed
96
	 * @throws ConfigException
97
	 */
98
	public function get( string $opt ) {
99
		if ( !isset( $this->opts[ $opt ] ) ) {
100
			throw new ConfigException( "Config option '$opt' not set." );
101
		}
102
		return $this->opts[ $opt ];
103
	}
104
}
105