Passed
Push — master ( 390609...8584e7 )
by Daimona
02:06
created

ContextSource   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 90
rs 10
wmc 10

10 Methods

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