UnblockIp::handle()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 1
nop 0
1
<?php
2
3
namespace Spinzar\Firewall\Commands;
4
5
use Spinzar\Firewall\Models\Ip;
6
use Carbon\Carbon;
7
use Illuminate\Console\Command;
8
9
class UnblockIp extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'firewall:unblockip';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Unblock ips based on their block period';
24
25
    /**
26
     * Create a new command instance.
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        $now = Carbon::now(config('app.timezone'));
41
42
        $ip = config('firewall.models.ip', Ip::class);
43
        $ip::with('log')->blocked()->each(function ($ip) use ($now) {
44
            if (empty($ip->log)) {
45
                return;
46
            }
47
48
            $period = config('firewall.middleware.' . $ip->log->middleware . '.auto_block.period');
49
50
            if ($ip->created_at->addSeconds($period) > $now) {
51
                return;
52
            }
53
54
            $ip->logs()->delete();
55
            $ip->delete();
56
        });
57
    }
58
}
59