Completed
Push — search ( 076ba7...2a5267 )
by Simon
16:36 queued 11:48
created

ApplicationBase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 3
    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 3
        $this->configuration = $configuration;
33 3
    }
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
        $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 1
    public function getConfiguration()
93
    {
94 1
        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 = null
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($siteConfiguration);
134
135
        $page->setEmailHelper(new EmailHelper());
136
        $page->setHttpHelper($httpHelper);
137
        $page->setWikiTextHelper(new WikiTextHelper($siteConfiguration, $page->getHttpHelper()));
138
139
        if ($siteConfiguration->getLocationProviderApiKey() === null) {
140
            $page->setLocationProvider(new FakeLocationProvider());
141
        }
142
        else {
143
            $page->setLocationProvider(
144
                new IpLocationProvider(
145
                    $database,
146
                    $siteConfiguration->getLocationProviderApiKey(),
147
                    $httpHelper
148
                ));
149
        }
150
151
        $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database));
152
153
        $page->setRdnsProvider(new CachedRDnsLookupProvider($database));
154
155
        $page->setAntiSpoofProvider(new CachedApiAntispoofProvider(
156
            $database,
157
            $this->getConfiguration()->getMediawikiWebServiceEndpoint(),
158
            $httpHelper));
159
160
        $page->setOAuthProtocolHelper(new OAuthProtocolHelper(
161
            $siteConfiguration->getOAuthBaseUrl(),
162
            $siteConfiguration->getOAuthConsumerToken(),
163
            $siteConfiguration->getOAuthConsumerSecret(),
164
            $siteConfiguration->getMediawikiWebServiceEndpoint()
165
        ));
166
167
        $page->setNotificationHelper(new IrcNotificationHelper(
168
            $siteConfiguration,
169
            $database,
170
            $notificationsDatabase));
171
172
        $page->setTorExitProvider(new TorExitProvider($database));
173
    }
174
}
175