Completed
Pull Request — master (#150)
by
unknown
06:02
created

ServiceProvider   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 315
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 24

Test Coverage

Coverage 95.87%

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 24
dl 0
loc 315
ccs 116
cts 121
cp 0.9587
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getRootDirectory() 0 4 1
A registerAttackBlocker() 0 6 1
A registerCountriesRepository() 0 6 1
A registerBlacklistCommand() 0 8 1
A registerCache() 0 6 1
A registerClearCommand() 0 8 1
A registerDataRepository() 0 6 1
A registerEventListeners() 0 4 1
A registerFirewall() 0 18 1
A registerIpAddress() 0 6 1
A registerIpList() 0 6 1
A registerMessageRepository() 0 6 1
A registerMigrations() 0 4 1
A registerGeoIp() 0 6 1
A registerRemoveCommand() 0 8 1
A registerReportCommand() 0 8 1
A registerUpdateGeoIpCommand() 0 8 1
A registerWhitelistCommand() 0 8 1
A getFirewallModel() 0 8 2
A register() 0 45 3
A registerFileSystem() 0 6 1
A registerMiddleware() 0 10 1
1
<?php
2
3
namespace PragmaRX\Firewall\Vendor\Laravel;
4
5
use Illuminate\Support\Facades\Event;
6
use PragmaRX\Firewall\Events\AttackDetected;
7
use PragmaRX\Firewall\Exceptions\ConfigurationOptionNotAvailable;
8
use PragmaRX\Firewall\Filters\Blacklist;
9
use PragmaRX\Firewall\Filters\Whitelist;
10
use PragmaRX\Firewall\Firewall;
11
use PragmaRX\Firewall\Listeners\NotifyAdmins;
12
use PragmaRX\Firewall\Middleware\FirewallBlacklist;
13
use PragmaRX\Firewall\Middleware\FirewallWhitelist;
14
use PragmaRX\Firewall\Repositories\Cache\Cache;
15
use PragmaRX\Firewall\Repositories\Countries;
16
use PragmaRX\Firewall\Repositories\DataRepository;
17
use PragmaRX\Firewall\Repositories\IpList;
18
use PragmaRX\Firewall\Repositories\Message;
19
use PragmaRX\Firewall\Support\AttackBlocker;
20
use PragmaRX\Firewall\Support\IpAddress;
21
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Blacklist as BlacklistCommand;
22
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Clear as ClearCommand;
23
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Remove as RemoveCommand;
24
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Report as ReportCommand;
25
use PragmaRX\Firewall\Vendor\Laravel\Artisan\UpdateGeoIp as UpdateGeoIpCommand;
26
use PragmaRX\Firewall\Vendor\Laravel\Artisan\Whitelist as WhitelistCommand;
27
use PragmaRX\Support\Filesystem;
28
use PragmaRX\Support\GeoIp\GeoIp;
29
use PragmaRX\Support\ServiceProvider as PragmaRXServiceProvider;
30
31
class ServiceProvider extends PragmaRXServiceProvider
32
{
33
    protected $packageVendor = 'pragmarx';
34
35
    protected $packageVendorCapitalized = 'PragmaRX';
36
37
    protected $packageName = 'firewall';
38
39
    protected $packageNameCapitalized = 'Firewall';
40
41
    private $firewall;
42
43
    /**
44
     * Get the full path of the stub config file.
45
     *
46
     * @throws ConfigurationOptionNotAvailable
47
     *
48
     * @return \Illuminate\Database\Eloquent\Model
49
     */
50 60
    private function getFirewallModel()
51
    {
52 60
        if (!$firewallModel = $this->getConfig('firewall_model')) {
53
            throw new ConfigurationOptionNotAvailable('Config option "firewall_model" is not available, please publish/check your configuration.');
54
        }
55
56 60
        return new $firewallModel();
57
    }
58
59
    /**
60
     * Get the root directory for this ServiceProvider.
61
     *
62
     * @return string
63
     */
64 72
    public function getRootDirectory()
65
    {
66 72
        return __DIR__.'/../..';
67
    }
68
69
    /**
70
     * Register the service provider.
71
     *
72
     * @return void
73
     */
74 72
    public function register()
75
    {
76 72
        parent::register();
77
78 72
        if (!$this->getConfig('enabled')) {
79
            return;
80
        }
81
82 72
        $this->registerMigrations();
83
84 72
        $this->registerFileSystem();
85
86 72
        $this->registerCache();
87
88 72
        $this->registerFirewall();
89
90 72
        $this->registerDataRepository();
91
92 72
        $this->registerMessageRepository();
93
94 72
        $this->registerIpList();
95
96 72
        $this->registerIpAddress();
97
98 72
        $this->registerGeoIp();
99
100 72
        $this->registerAttackBlocker();
101
102 72
        $this->registerReportCommand();
103
104 72
        $this->registerCountriesRepository();
105
106 72
        if ($this->getConfig('use_database')) {
107 24
            $this->registerWhitelistCommand();
108 24
            $this->registerBlacklistCommand();
109 24
            $this->registerRemoveCommand();
110 24
            $this->registerClearCommand();
111
        }
112
113 72
        $this->registerUpdateGeoIpCommand();
114
115 72
        $this->registerMiddleware();
116
117 72
        $this->registerEventListeners();
118 72
    }
119
120
    /**
121
     * Register the attack blocker.
122
     */
123 72
    private function registerAttackBlocker()
124
    {
125
        $this->app->singleton('firewall.attackBlocker', function () {
126 72
            return new AttackBlocker();
127 72
        });
128 72
    }
129
130
    /**
131
     * Register the countries repository.
132
     */
133 72
    private function registerCountriesRepository()
134
    {
135
        $this->app->singleton('firewall.countries', function () {
136 35
            return new Countries();
137 72
        });
138 72
    }
139
140
    /**
141
     * Register the Blacklist Artisan command.
142
     *
143
     * @return void
144
     */
145 24
    private function registerBlacklistCommand()
146
    {
147
        $this->app->singleton('firewall.blacklist.command', function () {
148 24
            return new BlacklistCommand();
149 24
        });
150
151 24
        $this->commands('firewall.blacklist.command');
152 24
    }
153
154
    /**
155
     * Register the Cache driver used by Firewall.
156
     *
157
     * @return void
158
     */
159 72
    private function registerCache()
160
    {
161
        $this->app->singleton('firewall.cache', function () {
162 72
            return new Cache(app('cache'));
163 72
        });
164 72
    }
165
166
    /**
167
     * Register the List Artisan command.
168
     *
169
     * @return void
170
     */
171 24
    private function registerClearCommand()
172
    {
173
        $this->app->singleton('firewall.clear.command', function () {
174 24
            return new ClearCommand();
175 24
        });
176
177 24
        $this->commands('firewall.clear.command');
178 24
    }
179
180
    /**
181
     * Register the Data Repository driver used by Firewall.
182
     *
183
     * @return void
184
     */
185 72
    private function registerDataRepository()
186
    {
187
        $this->app->singleton('firewall.datarepository', function () {
188 72
            return new DataRepository();
189 72
        });
190 72
    }
191
192
    /**
193
     * Register event listeners.
194
     */
195 72
    private function registerEventListeners()
196
    {
197 72
        Event::listen(AttackDetected::class, NotifyAdmins::class);
198 72
    }
199
200
    /**
201
     * Register the Filesystem driver used by Firewall.
202
     *
203
     * @return void
204
     */
205 72
    private function registerFileSystem()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
206
    {
207
        $this->app->singleton('firewall.filesystem', function () {
208
            return new Filesystem();
209 72
        });
210 72
    }
211
212
    /**
213
     * Takes all the components of Firewall and glues them
214
     * together to create Firewall.
215
     *
216
     * @return void
217
     */
218 72
    private function registerFirewall()
219
    {
220
        $this->app->singleton('firewall', function ($app) {
221 72
            $app['firewall.loaded'] = true;
222
223 72
            $this->firewall = new Firewall(
224 72
                $app['firewall.config'],
225 72
                $app['firewall.datarepository'],
226 72
                $app['request'],
227 72
                $attackBlocker = $app['firewall.attackBlocker'],
228 72
                $app['firewall.messages']
229
            );
230
231 72
            $attackBlocker->setFirewall($this->firewall);
232
233 72
            return $this->firewall;
234 72
        });
235 72
    }
236
237 72
    private function registerIpAddress()
238
    {
239
        $this->app->singleton('firewall.ipaddress', function () {
240 57
            return new IpAddress();
241 72
        });
242 72
    }
243
244
    /**
245
     * Register the ip list repository.
246
     */
247 72
    private function registerIpList()
248
    {
249
        $this->app->singleton('firewall.iplist', function () {
250 60
            return new IpList($this->getFirewallModel());
0 ignored issues
show
Compatibility introduced by
$this->getFirewallModel() of type object<Illuminate\Database\Eloquent\Model> is not a sub-type of object<PragmaRX\Firewall...aravel\Models\Firewall>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Model to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
251 72
        });
252 72
    }
253
254
    /**
255
     * Register the message repository.
256
     */
257 72
    private function registerMessageRepository()
258
    {
259
        $this->app->singleton('firewall.messages', function () {
260 72
            return new Message();
261 72
        });
262 72
    }
263
264
    /**
265
     * Register blocking and unblocking Middleware.
266
     *
267
     * @return void
268
     */
269 72
    private function registerMiddleware()
270
    {
271
        $this->app->singleton('firewall.middleware.blacklist', function () {
272
            return new FirewallBlacklist(new Blacklist());
273 72
        });
274
275
        $this->app->singleton('firewall.middleware.whitelist', function () {
276
            return new FirewallWhitelist(new Whitelist());
277 72
        });
278 72
    }
279
280 72
    private function registerMigrations()
281
    {
282 72
        $this->loadMigrationsFrom(__DIR__.'/../../migrations');
283 72
    }
284
285 72
    private function registerGeoIp()
286
    {
287
        $this->app->singleton('firewall.geoip', function () {
288 28
            return new GeoIp($this->getConfig('geoip_database_path'));
289 72
        });
290 72
    }
291
292
    /**
293
     * Register the List Artisan command.
294
     *
295
     * @return void
296
     */
297 24
    private function registerRemoveCommand()
298
    {
299
        $this->app->singleton('firewall.remove.command', function () {
300 24
            return new RemoveCommand();
301 24
        });
302
303 24
        $this->commands('firewall.remove.command');
304 24
    }
305
306
    /**
307
     * Register the List Artisan command.
308
     *
309
     * @return void
310
     */
311 72
    private function registerReportCommand()
312
    {
313
        $this->app->singleton('firewall.list.command', function () {
314 72
            return new ReportCommand();
315 72
        });
316
317 72
        $this->commands('firewall.list.command');
318 72
    }
319
320
    /**
321
     * Register the updategeoip command.
322
     */
323 72
    private function registerUpdateGeoIpCommand()
324
    {
325
        $this->app->singleton('firewall.updategeoip.command', function () {
326 72
            return new UpdateGeoIpCommand();
327 72
        });
328
329 72
        $this->commands('firewall.updategeoip.command');
330 72
    }
331
332
    /**
333
     * Register the Whitelist Artisan command.
334
     *
335
     * @return void
336
     */
337 24
    private function registerWhitelistCommand()
338
    {
339
        $this->app->singleton('firewall.whitelist.command', function () {
340 24
            return new WhitelistCommand();
341 24
        });
342
343 24
        $this->commands('firewall.whitelist.command');
344 24
    }
345
}
346