Failed Conditions
Push — newinternal ( 216d62...410e59 )
by Simon
05:28 queued 13s
created

ApplicationBase::setupDatabase()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
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\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\TorExitProvider;
23
use Waca\Providers\XffTrustProvider;
24
use Waca\Tasks\ITask;
25
26
abstract class ApplicationBase
27
{
28
    private $configuration;
29
30
    public function __construct(SiteConfiguration $configuration)
0 ignored issues
show
Bug introduced by
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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