Completed
Push — master ( 592d95...c00565 )
by Abdelrahman
18:11 queued 10s
created

PurgeCommand::handle()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5866
c 0
b 0
f 0
cc 7
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Console\Commands;
6
7
use Illuminate\Support\Carbon;
8
use Illuminate\Console\Command;
9
10
class PurgeCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'rinvex:oauth:purge
18
                            {--revoked : Only purge revoked tokens and authentication codes}
19
                            {--expired : Only purge expired tokens and authentication codes}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Purge revoked and / or expired tokens and authentication codes';
27
28
    /**
29
     * Execute the console command.
30
     */
31
    public function handle()
32
    {
33
        $expired = Carbon::now()->subDays(7);
34
35
        if (($this->option('revoked') && $this->option('expired')) ||
36
            (! $this->option('revoked') && ! $this->option('expired'))) {
37
            app('rinvex.oauth.access_token')->where('is_revoked', true)->orWhereDate('expires_at', '<', $expired)->delete();
38
            app('rinvex.oauth.auth_code')->where('is_revoked', true)->orWhereDate('expires_at', '<', $expired)->delete();
39
            app('rinvex.oauth.refresh_token')->where('is_revoked', true)->orWhereDate('expires_at', '<', $expired)->delete();
40
41
            $this->info('Purged revoked items and items expired for more than seven days.');
42
        } elseif ($this->option('revoked')) {
43
            app('rinvex.oauth.access_token')->where('is_revoked', true)->delete();
44
            app('rinvex.oauth.auth_code')->where('is_revoked', true)->delete();
45
            app('rinvex.oauth.refresh_token')->where('is_revoked', true)->delete();
46
47
            $this->info('Purged revoked items.');
48
        } elseif ($this->option('expired')) {
49
            app('rinvex.oauth.access_token')->whereDate('expires_at', '<', $expired)->delete();
50
            app('rinvex.oauth.auth_code')->whereDate('expires_at', '<', $expired)->delete();
51
            app('rinvex.oauth.refresh_token')->whereDate('expires_at', '<', $expired)->delete();
52
53
            $this->info('Purged items expired for more than seven days.');
54
        }
55
    }
56
}
57