Passed
Pull Request — multiproject/requestforms (#741)
by Simon
14:49 queued 10:35
created

ApplicationBase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 10
eloc 44
dl 0
loc 143
ccs 5
cts 45
cp 0.1111
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfiguration() 0 3 1
A setupEnvironment() 0 5 1
A setupDatabase() 0 11 2
A __construct() 0 3 1
A run() 0 12 3
A setupHelpers() 0 49 2
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\OAuthProtocolHelper;
17
use Waca\Providers\CachedApiAntispoofProvider;
18
use Waca\Providers\CachedRDnsLookupProvider;
19
use Waca\Providers\FakeLocationProvider;
20
use Waca\Providers\IpLocationProvider;
21
use Waca\Providers\TorExitProvider;
22
use Waca\Providers\XffTrustProvider;
23
use Waca\Tasks\ITask;
24
25
abstract class ApplicationBase
26
{
27
    private $configuration;
28
29 3
    public function __construct(SiteConfiguration $configuration)
30
    {
31 3
        $this->configuration = $configuration;
32 3
    }
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
        $actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn();
81
        if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) {
82
            throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.');
83
        }
84
85
        return $database;
86
    }
87
88
    /**
89
     * @return SiteConfiguration
90
     */
91 1
    public function getConfiguration()
92
    {
93 1
        return $this->configuration;
94
    }
95
96
    /**
97
     * Main application logic
98
     * @return void
99
     */
100
    abstract protected function main();
101
102
    /**
103
     * Any cleanup tasks should go here
104
     *
105
     * Note that we need to be very careful here, as exceptions may have been thrown and handled.
106
     * This should *only* be for cleaning up, no logic should go here.
107
     *
108
     * @return void
109
     */
110
    abstract protected function cleanupEnvironment();
111
112
    /**
113
     * @param ITask             $page
114
     * @param SiteConfiguration $siteConfiguration
115
     * @param PdoDatabase       $database
116
     *
117
     * @return void
118
     */
119
    protected function setupHelpers(
120
        ITask $page,
121
        SiteConfiguration $siteConfiguration,
122
        PdoDatabase $database
123
    ) {
124
        $page->setSiteConfiguration($siteConfiguration);
125
126
        // setup the global database object
127
        $page->setDatabase($database);
128
129
        // set up helpers and inject them into the page.
130
        $httpHelper = new HttpHelper($siteConfiguration);
131
132
        $page->setEmailHelper(
133
            new EmailHelper($siteConfiguration->getEmailSender(), $siteConfiguration->getIrcNotificationsInstance())
134
        );
135
136
        $page->setHttpHelper($httpHelper);
137
138
        if ($siteConfiguration->getLocationProviderApiKey() === null) {
139
            $page->setLocationProvider(new FakeLocationProvider());
140
        }
141
        else {
142
            $page->setLocationProvider(
143
                new IpLocationProvider(
144
                    $database,
145
                    $siteConfiguration->getLocationProviderApiKey(),
146
                    $httpHelper
147
                ));
148
        }
149
150
        $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database));
151
152
        $page->setRdnsProvider(new CachedRDnsLookupProvider($database));
153
154
        $page->setAntiSpoofProvider(new CachedApiAntispoofProvider($database, $httpHelper));
155
156
        $page->setOAuthProtocolHelper(new OAuthProtocolHelper(
157
            $siteConfiguration->getOAuthConsumerToken(),
158
            $siteConfiguration->getOAuthConsumerSecret(),
159
            $database,
160
            $siteConfiguration->getUserAgent()
161
        ));
162
163
        $page->setNotificationHelper(new IrcNotificationHelper(
164
            $siteConfiguration,
165
            $database));
166
167
        $page->setTorExitProvider(new TorExitProvider($database));
168
    }
169
}
170