Passed
Push — master ( 309b86...ef711e )
by Darko
10:33
created

DisableExpiredPromotions   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 18
c 1
b 0
f 0
dl 0
loc 44
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 3
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\RolePromotion;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Carbon;
8
9
class DisableExpiredPromotions extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'nntmux:disable-expired-promotions';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Disable role promotions that have passed their end date';
24
25
    /**
26
     * Execute the console command.
27
     */
28
    public function handle(): int
29
    {
30
        $now = Carbon::now();
31
32
        // Find all active promotions that have passed their end date
33
        $expiredPromotions = RolePromotion::where('is_active', true)
34
            ->whereNotNull('end_date')
35
            ->where('end_date', '<', $now)
36
            ->get();
37
38
        if ($expiredPromotions->isEmpty()) {
39
            $this->info('No expired promotions found.');
40
            return Command::SUCCESS;
41
        }
42
43
        $count = 0;
44
        foreach ($expiredPromotions as $promotion) {
45
            $promotion->update(['is_active' => false]);
46
            $this->line("Disabled promotion: {$promotion->name} (ended: {$promotion->end_date->format('Y-m-d')})");
47
            $count++;
48
        }
49
50
        $this->info("Successfully disabled {$count} expired promotion(s).");
51
52
        return Command::SUCCESS;
53
    }
54
}
55
56