Completed
Branch newinternal (830cd7)
by Simon
03:46
created

ApplicationBase::setupHelpers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 55
ccs 0
cts 36
cp 0
rs 9.7692
cc 2
eloc 37
nc 2
nop 4
crap 6

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