BlockIp   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 2
1
<?php
2
3
namespace Spinzar\Firewall\Listeners;
4
5
use Spinzar\Firewall\Events\AttackDetected;
6
use Spinzar\Firewall\Models\Ip;
7
use Spinzar\Firewall\Models\Log;
8
use Carbon\Carbon;
9
10
class BlockIp
11
{
12
    /**
13
     * Handle the event.
14
     *
15
     * @param AttackDetected $event
16
     *
17
     * @return void
18
     */
19
    public function handle(AttackDetected $event)
20
    {
21
        $end = Carbon::now(config('app.timezone'));
22
        $start = $end->copy()->subSeconds(config('firewall.middleware.' . $event->log->middleware . '.auto_block.frequency'));
23
24
        $log = config('firewall.models.log', Log::class);
25
        $count = $log::where('ip', $event->log->ip)->whereBetween('created_at', [$start, $end])->count();
26
27
        if ($count != config('firewall.middleware.' . $event->log->middleware . '.auto_block.attempts')) {
28
            return;
29
        }
30
31
        $ip = config('firewall.models.ip', Ip::class);
32
        $ip::create([
33
            'ip' => $event->log->ip,
34
            'log_id' => $event->log->id,
35
        ]);
36
    }
37
}
38