Passed
Push — oauth-migration-tweaks ( 30d8c1...fb3afb )
by Simon
17:22 queued 13:26
created

ApplicationBase::setupHelpers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 51
ccs 0
cts 25
cp 0
rs 9.456
cc 2
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\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
     * @param PdoDatabase       $notificationsDatabase
117
     *
118
     * @return void
119
     */
120
    protected function setupHelpers(
121
        ITask $page,
122
        SiteConfiguration $siteConfiguration,
123
        PdoDatabase $database,
124
        PdoDatabase $notificationsDatabase = null
125
    ) {
126
        $page->setSiteConfiguration($siteConfiguration);
127
128
        // setup the global database object
129
        $page->setDatabase($database);
130
131
        // set up helpers and inject them into the page.
132
        $httpHelper = new HttpHelper($siteConfiguration);
133
134
        $page->setEmailHelper(new EmailHelper());
135
        $page->setHttpHelper($httpHelper);
136
137
        if ($siteConfiguration->getLocationProviderApiKey() === null) {
138
            $page->setLocationProvider(new FakeLocationProvider());
139
        }
140
        else {
141
            $page->setLocationProvider(
142
                new IpLocationProvider(
143
                    $database,
144
                    $siteConfiguration->getLocationProviderApiKey(),
145
                    $httpHelper
146
                ));
147
        }
148
149
        $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database));
150
151
        $page->setRdnsProvider(new CachedRDnsLookupProvider($database));
152
153
        $page->setAntiSpoofProvider(new CachedApiAntispoofProvider(
154
            $database,
155
            $this->getConfiguration()->getMediawikiWebServiceEndpoint(),
156
            $httpHelper));
157
158
        $page->setOAuthProtocolHelper(new OAuthProtocolHelper(
159
            $siteConfiguration->getOAuthBaseUrl(),
160
            $siteConfiguration->getOAuthConsumerToken(),
161
            $siteConfiguration->getOAuthConsumerSecret(),
162
            $siteConfiguration->getMediawikiWebServiceEndpoint()
163
        ));
164
165
        $page->setNotificationHelper(new IrcNotificationHelper(
166
            $siteConfiguration,
167
            $database,
168
            $notificationsDatabase));
169
170
        $page->setTorExitProvider(new TorExitProvider($database));
171
    }
172
}
173