Passed
Push — master ( 8fed13...72e6d1 )
by Daimona
02:00
created

ContextSource::getMessageProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Message\Message;
6
use BotRiconferme\Request\RequestFactory;
7
use BotRiconferme\Wiki\Page\Page;
8
use BotRiconferme\Wiki\Page\PageBotList;
9
use BotRiconferme\Wiki\User;
10
use BotRiconferme\Wiki\Wiki;
11
use BotRiconferme\Wiki\WikiGroup;
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * Base class with a few utility methods available to get a logger, the config and a wiki
17
 */
18
abstract class ContextSource implements LoggerAwareInterface {
19
	/** @var LoggerInterface */
20
	private $logger;
21
22
	/** @var Config */
23
	private $config;
24
25
	/** @var WikiGroup */
26
	private $wikiGroup;
27
28
	/** @var MessageProvider */
29
	private $messageProvider;
30
31
	/** @var PageBotList */
32
	private $pageBotList;
33
34
	/**
35
	 * @param LoggerInterface $logger
36
	 * @param WikiGroup $wikiGroup
37
	 * @param MessageProvider $mp
38
	 * @param PageBotList $pbl
39
	 */
40
	public function __construct(
41
		LoggerInterface $logger,
42
		WikiGroup $wikiGroup,
43
		MessageProvider $mp,
44
		PageBotList $pbl
45
	) {
46
		$this->setLogger( $logger );
47
		$this->setConfig( Config::getInstance() );
48
		$this->setWikiGroup( $wikiGroup );
49
		$this->setMessageProvider( $mp );
50
		$this->pageBotList = $pbl;
51
	}
52
53
	/**
54
	 * @return LoggerInterface
55
	 */
56
	protected function getLogger(): LoggerInterface {
57
		return $this->logger;
58
	}
59
60
	/**
61
	 * @inheritDoc
62
	 */
63
	public function setLogger( LoggerInterface $logger ): void {
64
		$this->logger = $logger;
65
	}
66
67
	/**
68
	 * Shorthand to $this->getConfig()->get
69
	 *
70
	 * @param string $optname
71
	 * @return mixed
72
	 */
73
	protected function getOpt( string $optname ) {
74
		return $this->getConfig()->get( $optname );
75
	}
76
77
	/**
78
	 * @return Config
79
	 */
80
	protected function getConfig(): Config {
81
		return $this->config;
82
	}
83
84
	/**
85
	 * @param Config $cfg
86
	 */
87
	protected function setConfig( Config $cfg ): void {
88
		$this->config = $cfg;
89
	}
90
91
	/**
92
	 * Shorthand
93
	 * @return Wiki
94
	 */
95
	protected function getWiki(): Wiki {
96
		return $this->getWikiGroup()->getMainWiki();
97
	}
98
99
	/**
100
	 * @return WikiGroup
101
	 */
102
	protected function getWikiGroup(): WikiGroup {
103
		return $this->wikiGroup;
104
	}
105
106
	/**
107
	 * @param WikiGroup $wikiGroup
108
	 */
109
	protected function setWikiGroup( WikiGroup $wikiGroup ): void {
110
		$this->wikiGroup = $wikiGroup;
111
	}
112
113
	/**
114
	 * @return MessageProvider
115
	 */
116
	protected function getMessageProvider(): MessageProvider {
117
		return $this->messageProvider;
118
	}
119
120
	/**
121
	 * @param MessageProvider $mp
122
	 */
123
	protected function setMessageProvider( MessageProvider $mp ): void {
124
		$this->messageProvider = $mp;
125
	}
126
127
	/**
128
	 * Get a message
129
	 *
130
	 * @param string $key
131
	 * @return Message
132
	 */
133
	protected function msg( string $key ): Message {
134
		return $this->messageProvider->getMessage( $key );
135
	}
136
137
	/**
138
	 * @return PageBotList
139
	 */
140
	public function getBotList(): PageBotList {
141
		return $this->pageBotList;
142
	}
143
144
	/**
145
	 * Shorthand to get a page using the local wiki
146
	 *
147
	 * @param string $title
148
	 * @return Page
149
	 */
150
	protected function getPage( string $title ): Page {
151
		return new Page( $title, $this->getWiki() );
152
	}
153
154
	/**
155
	 * Shorthand to get a user using the local wiki
156
	 *
157
	 * @param string $name
158
	 * @return User
159
	 */
160
	protected function getUser( string $name ): User {
161
		$ui = $this->getBotList()->getUserInfo( $name );
162
		return new User( $ui, $this->getWiki() );
163
	}
164
}
165