Completed
Push — master ( 85b241...3d718b )
by Antonio Carlos
05:58 queued 02:30
created

ServiceProvider   C

Complexity

Total Complexity 25

Size/Duplication

Total Lines 315
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 23

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 4 Features 0
Metric Value
wmc 25
lcom 1
cbo 23
dl 0
loc 315
ccs 120
cts 120
cp 1
rs 5.5
c 10
b 4
f 0

22 Methods

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