ContextSource   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 145
rs 10
wmc 15

15 Methods

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