Passed
Branch master (a88228)
by Antonio Carlos
03:04
created

ServiceProvider   C

Complexity

Total Complexity 27

Size/Duplication

Total Lines 335
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Test Coverage

Coverage 94.35%

Importance

Changes 15
Bugs 4 Features 0
Metric Value
wmc 27
lcom 1
cbo 23
dl 0
loc 335
ccs 117
cts 124
cp 0.9435
rs 5.5
c 15
b 4
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirewallModel() 0 8 2
A getPackageDir() 0 4 1
A getRootDirectory() 0 4 1
A provides() 0 4 1
B register() 0 45 3
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 registerMiddleware() 0 10 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 registerFileSystem() 0 6 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 26
    private function getFirewallModel()
51
    {
52 26
        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 26
        return new $firewallModel();
57
    }
58
59
    /**
60
     * Get the current package directory.
61
     *
62
     * @return string
63
     */
64
    public function getPackageDir()
65
    {
66
        return __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..';
67
    }
68
69
    /**
70
     * Get the root directory for this ServiceProvider.
71
     *
72
     * @return string
73
     */
74 40
    public function getRootDirectory()
75
    {
76 40
        return __DIR__.'/../..';
77
    }
78
79
    /**
80
     * Get the services provided by the provider.
81
     *
82
     * @return array
83
     */
84
    public function provides()
85
    {
86
        return ['firewall'];
87
    }
88
89
    /**
90
     * Register the service provider.
91
     *
92
     * @return void
93
     */
94 40
    public function register()
95
    {
96 40
        parent::register();
97
98 40
        if (!$this->getConfig('enabled')) {
99 1
            return;
100
        }
101
102 39
        $this->registerMigrations();
103
104 39
        $this->registerFileSystem();
105
106 39
        $this->registerCache();
107
108 39
        $this->registerFirewall();
109
110 39
        $this->registerDataRepository();
111
112 39
        $this->registerMessageRepository();
113
114 39
        $this->registerIpList();
115
116 39
        $this->registerIpAddress();
117
118 39
        $this->registerGeoIp();
119
120 39
        $this->registerAttackBlocker();
121
122 39
        $this->registerReportCommand();
123
124 39
        $this->registerCountriesRepository();
125
126 39
        if ($this->getConfig('use_database')) {
127 15
            $this->registerWhitelistCommand();
128 15
            $this->registerBlacklistCommand();
129 15
            $this->registerRemoveCommand();
130 15
            $this->registerClearCommand();
131
        }
132
133 39
        $this->registerUpdateGeoIpCommand();
134
135 39
        $this->registerMiddleware();
136
137 39
        $this->registerEventListeners();
138 39
    }
139
140
    /**
141
     * Register the attack blocker.
142
     */
143
    private function registerAttackBlocker()
144
    {
145 39
        $this->app->singleton('firewall.attackBlocker', function () {
146 38
            return new AttackBlocker();
147 39
        });
148 39
    }
149
150
    /**
151
     * Register the countries repository.
152
     */
153
    private function registerCountriesRepository()
154
    {
155 39
        $this->app->singleton('firewall.countries', function () {
156 12
            return new Countries();
157 39
        });
158 39
    }
159
160
    /**
161
     * Register the Blacklist Artisan command.
162
     *
163
     * @return void
164
     */
165
    private function registerBlacklistCommand()
166
    {
167 15
        $this->app->singleton('firewall.blacklist.command', function () {
168 15
            return new BlacklistCommand();
169 15
        });
170
171 15
        $this->commands('firewall.blacklist.command');
172 15
    }
173
174
    /**
175
     * Register the Cache driver used by Firewall.
176
     *
177
     * @return void
178
     */
179
    private function registerCache()
180
    {
181 39
        $this->app->singleton('firewall.cache', function () {
182 24
            return new Cache(app('cache'));
183 39
        });
184 39
    }
185
186
    /**
187
     * Register the List Artisan command.
188
     *
189
     * @return void
190
     */
191
    private function registerClearCommand()
192
    {
193 15
        $this->app->singleton('firewall.clear.command', function () {
194 15
            return new ClearCommand();
195 15
        });
196
197 15
        $this->commands('firewall.clear.command');
198 15
    }
199
200
    /**
201
     * Register the Data Repository driver used by Firewall.
202
     *
203
     * @return void
204
     */
205
    private function registerDataRepository()
206
    {
207 39
        $this->app->singleton('firewall.datarepository', function () {
208 38
            return new DataRepository();
209 39
        });
210 39
    }
211
212
    /**
213
     * Register event listeners.
214
     */
215 39
    private function registerEventListeners()
216
    {
217 39
        Event::listen(AttackDetected::class, NotifyAdmins::class);
218 39
    }
219
220
    /**
221
     * Register the Filesystem driver used by Firewall.
222
     *
223
     * @return void
224
     */
225
    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...
226
    {
227 39
        $this->app->singleton('firewall.filesystem', function () {
228 1
            return new Filesystem();
229 39
        });
230 39
    }
231
232
    /**
233
     * Takes all the components of Firewall and glues them
234
     * together to create Firewall.
235
     *
236
     * @return void
237
     */
238
    private function registerFirewall()
239
    {
240 39
        $this->app->singleton('firewall', function ($app) {
241 38
            $app['firewall.loaded'] = true;
242
243 38
            $this->firewall = new Firewall(
244 38
                $app['firewall.config'],
245 38
                $app['firewall.datarepository'],
246 38
                $app['request'],
247 38
                $attackBlocker = $app['firewall.attackBlocker'],
248 38
                $app['firewall.messages']
249
            );
250
251 38
            $attackBlocker->setFirewall($this->firewall);
252
253 38
            return $this->firewall;
254 39
        });
255 39
    }
256
257
    private function registerIpAddress()
258
    {
259 39
        $this->app->singleton('firewall.ipaddress', function () {
260 26
            return new IpAddress();
261 39
        });
262 39
    }
263
264
    /**
265
     * Register the ip list repository.
266
     */
267
    private function registerIpList()
268
    {
269 39
        $this->app->singleton('firewall.iplist', function () {
270 26
            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...
271 39
        });
272 39
    }
273
274
    /**
275
     * Register the message repository.
276
     */
277
    private function registerMessageRepository()
278
    {
279 39
        $this->app->singleton('firewall.messages', function () {
280 38
            return new Message();
281 39
        });
282 39
    }
283
284
    /**
285
     * Register blocking and unblocking Middleware.
286
     *
287
     * @return void
288
     */
289
    private function registerMiddleware()
290
    {
291 39
        $this->app->singleton('firewall.middleware.blacklist', function () {
292
            return new FirewallBlacklist(new Blacklist());
293 39
        });
294
295 39
        $this->app->singleton('firewall.middleware.whitelist', function () {
296
            return new FirewallWhitelist(new Whitelist());
297 39
        });
298 39
    }
299
300 39
    private function registerMigrations()
301
    {
302 39
        $this->loadMigrationsFrom(__DIR__.'/../../migrations');
303 39
    }
304
305
    private function registerGeoIp()
306
    {
307 39
        $this->app->singleton('firewall.geoip', function () {
308 7
            return new GeoIp($this->getConfig('geoip_database_path'));
309 39
        });
310 39
    }
311
312
    /**
313
     * Register the List Artisan command.
314
     *
315
     * @return void
316
     */
317
    private function registerRemoveCommand()
318
    {
319 15
        $this->app->singleton('firewall.remove.command', function () {
320 15
            return new RemoveCommand();
321 15
        });
322
323 15
        $this->commands('firewall.remove.command');
324 15
    }
325
326
    /**
327
     * Register the List Artisan command.
328
     *
329
     * @return void
330
     */
331
    private function registerReportCommand()
332
    {
333 39
        $this->app->singleton('firewall.list.command', function () {
334 39
            return new ReportCommand();
335 39
        });
336
337 39
        $this->commands('firewall.list.command');
338 39
    }
339
340
    /**
341
     * Register the updategeoip command.
342
     */
343
    private function registerUpdateGeoIpCommand()
344
    {
345 39
        $this->app->singleton('firewall.updategeoip.command', function () {
346 39
            return new UpdateGeoIpCommand();
347 39
        });
348
349 39
        $this->commands('firewall.updategeoip.command');
350 39
    }
351
352
    /**
353
     * Register the Whitelist Artisan command.
354
     *
355
     * @return void
356
     */
357
    private function registerWhitelistCommand()
358
    {
359 15
        $this->app->singleton('firewall.whitelist.command', function () {
360 15
            return new WhitelistCommand();
361 15
        });
362
363
        $this->commands('firewall.whitelist.command');
364
    }
365
}
366