Passed
Push — master ( c20c86...5d9847 )
by Daimona
01:48
created

ContextSource::getUser()   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 1
dl 0
loc 2
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace BotRiconferme;
4
5
use BotRiconferme\Wiki\User;
6
use BotRiconferme\Wiki\Wiki;
7
use BotRiconferme\Wiki\Page\Page;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LoggerInterface;
10
11
/**
12
 * Base class with a few utility methods available to get a logger, the config and a wiki
13
 */
14
abstract class ContextSource implements LoggerAwareInterface {
15
	/** @var LoggerInterface */
16
	private $logger;
17
18
	/** @var Config */
19
	private $config;
20
21
	/** @var Wiki */
22
	private $wiki;
23
24
	/**
25
	 * @param LoggerInterface $logger
26
	 * @param Wiki $wiki
27
	 */
28
	public function __construct( LoggerInterface $logger, Wiki $wiki ) {
29
		$this->setLogger( $logger );
30
		$this->setConfig( Config::getInstance() );
31
		$this->setWiki( $wiki );
32
	}
33
34
	/**
35
	 * @return LoggerInterface
36
	 */
37
	protected function getLogger() : LoggerInterface {
38
		return $this->logger;
39
	}
40
41
	/**
42
	 * @inheritDoc
43
	 */
44
	public function setLogger( LoggerInterface $logger ) {
45
		$this->logger = $logger;
46
	}
47
48
	/**
49
	 * Shorthand to $this->getConfig()->get
50
	 *
51
	 * @param string $optname
52
	 * @return mixed
53
	 */
54
	protected function getOpt( string $optname ) {
55
		return $this->getConfig()->get( $optname );
56
	}
57
58
	/**
59
	 * @return Config
60
	 */
61
	protected function getConfig() : Config {
62
		return $this->config;
63
	}
64
65
	/**
66
	 * @param Config $cfg
67
	 */
68
	protected function setConfig( Config $cfg ) {
69
		$this->config = $cfg;
70
	}
71
72
	/**
73
	 * @return Wiki
74
	 */
75
	protected function getWiki() : Wiki {
76
		return $this->wiki;
77
	}
78
79
	/**
80
	 * @param Wiki $wiki
81
	 */
82
	protected function setWiki( Wiki $wiki ) {
83
		$this->wiki = $wiki;
84
	}
85
86
	/**
87
	 * Get a message
88
	 *
89
	 * @param string $key
90
	 * @return Message
91
	 */
92
	protected function msg( string $key ) : Message {
93
		return new Message( $key );
94
	}
95
96
	/**
97
	 * Shorthand to get a page using the local wiki
98
	 *
99
	 * @param string $title
100
	 * @return Page
101
	 */
102
	protected function getPage( string $title ) : Page {
103
		return new Page( $title, $this->getWiki() );
104
	}
105
106
	/**
107
	 * Shorthand to get a user using the local wiki
108
	 *
109
	 * @param string $name
110
	 * @return User
111
	 */
112
	protected function getUser( string $name ) : User {
113
		return new User( $name, $this->getWiki() );
114
	}
115
}
116