Completed
Branch newinternal (104de7)
by Simon
10:16
created

ApplicationBase::setupHelpers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 51
rs 9.4109
cc 2
eloc 34
nc 2
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Waca;
4
5
use Exception;
6
use Waca\Exceptions\EnvironmentException;
7
use Waca\Helpers\EmailHelper;
8
use Waca\Helpers\HttpHelper;
9
use Waca\Helpers\IrcNotificationHelper;
10
use Waca\Helpers\OAuthHelper;
11
use Waca\Helpers\WikiTextHelper;
12
use Waca\Providers\CachedApiAntispoofProvider;
13
use Waca\Providers\CachedRDnsLookupProvider;
14
use Waca\Providers\FakeLocationProvider;
15
use Waca\Providers\IpLocationProvider;
16
use Waca\Providers\XffTrustProvider;
17
use Waca\Tasks\ITask;
18
19
abstract class ApplicationBase
20
{
21
	private $configuration;
22
23
	public function __construct(SiteConfiguration $configuration)
24
	{
25
		$this->configuration = $configuration;
26
	}
27
28
	/**
29
	 * Application entry point.
30
	 *
31
	 * Sets up the environment and runs the application, performing any global cleanup operations when done.
32
	 */
33
	public function run()
34
	{
35
		try {
36
			if ($this->setupEnvironment()) {
37
				$this->main();
38
			}
39
		}
40
		catch (Exception $ex) {
41
			print $ex->getMessage();
42
		}
43
		finally {
44
			$this->cleanupEnvironment();
45
		}
46
	}
47
48
	/**
49
	 * Environment setup
50
	 *
51
	 * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false
52
	 * and shut down prematurely.
53
	 *
54
	 * @return bool
55
	 * @throws EnvironmentException
56
	 */
57
	protected function setupEnvironment()
58
	{
59
		$this->setupDatabase();
60
61
		return true;
62
	}
63
64
	/**
65
	 * @return PdoDatabase
66
	 * @throws EnvironmentException
67
	 * @throws Exception
68
	 */
69
	protected function setupDatabase()
70
	{
71
		// check the schema version
72
		$database = PdoDatabase::getDatabaseConnection('acc');
73
74
		/** @var int $actualVersion */
75
		$actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn();
76
		if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) {
77
			throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.');
78
		}
79
80
		return $database;
81
	}
82
83
	/**
84
	 * @return SiteConfiguration
85
	 */
86
	public function getConfiguration()
87
	{
88
		return $this->configuration;
89
	}
90
91
	/**
92
	 * Main application logic
93
	 * @return void
94
	 */
95
	abstract protected function main();
96
97
	/**
98
	 * Any cleanup tasks should go here
99
	 *
100
	 * Note that we need to be very careful here, as exceptions may have been thrown and handled.
101
	 * This should *only* be for cleaning up, no logic should go here.
102
	 *
103
	 * @return void
104
	 */
105
	abstract protected function cleanupEnvironment();
106
107
	/**
108
	 * @param ITask             $page
109
	 * @param SiteConfiguration $siteConfiguration
110
	 * @param PdoDatabase       $database
111
	 * @param PdoDatabase       $notificationsDatabase
112
	 *
113
	 * @return void
114
	 */
115
	protected function setupHelpers(
116
		ITask $page,
117
		SiteConfiguration $siteConfiguration,
118
		PdoDatabase $database,
119
		PdoDatabase $notificationsDatabase
120
	) {
121
		$page->setSiteConfiguration($siteConfiguration);
122
123
		// setup the global database object
124
		$page->setDatabase($database);
125
126
		// set up helpers and inject them into the page.
127
		$httpHelper = new HttpHelper(
128
			$siteConfiguration->getUserAgent(),
129
			$siteConfiguration->getCurlDisableVerifyPeer()
130
		);
131
132
		$page->setEmailHelper(new EmailHelper());
133
		$page->setHttpHelper($httpHelper);
134
		$page->setWikiTextHelper(new WikiTextHelper($siteConfiguration, $page->getHttpHelper()));
135
136
		if ($siteConfiguration->getLocationProviderApiKey() === null) {
137
			$page->setLocationProvider(new FakeLocationProvider());
138
		}
139
		else {
140
			$page->setLocationProvider(new IpLocationProvider($database,
141
				$siteConfiguration->getLocationProviderApiKey()));
142
		}
143
144
		$page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database));
145
146
		$page->setRdnsProvider(new CachedRDnsLookupProvider($database));
147
148
		$page->setAntiSpoofProvider(new CachedApiAntispoofProvider(
149
			$database,
150
			$this->getConfiguration()->getMediawikiWebServiceEndpoint(),
151
			$httpHelper));
152
153
		$page->setOAuthHelper(new OAuthHelper(
154
			$siteConfiguration->getOAuthBaseUrl(),
155
			$siteConfiguration->getOAuthConsumerToken(),
156
			$siteConfiguration->getOAuthConsumerSecret(),
157
			$httpHelper,
158
			$siteConfiguration->getMediawikiWebServiceEndpoint()
159
		));
160
161
		$page->setNotificationHelper(new IrcNotificationHelper(
162
			$siteConfiguration,
163
			$notificationsDatabase,
164
			$database));
165
	}
166
}