Kernel::commands()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace App\Console;
4
5
use App\Events\ExpiringPartnershipsEvent;
6
use App\Events\ExternalCoursesReportEvent;
7
use App\Events\MonthlyReportEvent;
8
use App\Models\Config;
9
use App\Models\Partner;
10
use App\Models\Period;
11
use App\Traits\HandlesAttendance;
12
use Carbon\Carbon;
13
use Illuminate\Console\Scheduling\Schedule;
14
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
15
use Illuminate\Support\Facades\Log;
16
17
class Kernel extends ConsoleKernel
18
{
19
    use HandlesAttendance;
0 ignored issues
show
Bug introduced by
The trait App\Traits\HandlesAttendance requires the property $email which is not provided by App\Console\Kernel.
Loading history...
20
21
    /**
22
     * The Artisan commands provided by your application.
23
     *
24
     * @var array
25
     */
26
    protected $commands = [
27
        //
28
    ];
29
30
    /**
31
     * Define the application's command schedule.
32
     *
33
     * @return void
34
     */
35
    protected function schedule(Schedule $schedule)
36
    {
37
        $schedule->call(function () {
38
            Log::info('Sending attendance reminders');
39
            $this->remindPendingAttendance();
40
        })->dailyAt('08:15');
41
42
        $schedule->call(function () {
43
            Log::info('Checking default periods');
44
45
            // when we finish the current period; remove the manual override to automatically fallbackto the next one
46
            $changeCurrentPeriod = Carbon::parse(Period::get_default_period()->end) < Carbon::now();
47
            if ($changeCurrentPeriod) {
48
                Log::info('Removing manual current period override');
49
                Config::where('name', 'current_period')->update(['value' => null]);
50
            }
51
52
            // if the enrollment period is the same as the current period, we can remove it
53
            if (Period::get_enrollments_period() == Period::get_default_period()) {
54
                Log::info('Removing manual enrollment period override');
55
                Config::where('name', 'default_enrollment_period')->update(['value' => null]);
56
            }
57
        })->dailyAt('00:00');
58
59
        if (config('settings.partnership_alerts')) {
60
            $schedule->call(function () {
61
                Log::info('Checking expired partnerships');
62
63
                // if one of the partnerships is expiring soon, send an email
64
                $partners = Partner::where(function ($query) {
65
                    $query->whereNotNull('expired_on')->where('expired_on', '<', Carbon::now()->addDays(28));
66
                })
67
                    ->where(function ($query) {
68
                        $query->whereNull('last_alert_sent_at')->orWhere('last_alert_sent_at', '>', Carbon::now()->subDays(28))->get();
69
                    });
70
71
                if ($partners->count() > 0) {
72
                    event(new ExpiringPartnershipsEvent($partners->get()));
73
                }
74
            })->dailyAt('02:05');
75
        }
76
77
        if (config('settings.external_courses_report')) {
78
            $schedule->call(function () {
79
                Log::info('Sending external courses reports');
80
                event(new ExternalCoursesReportEvent());
81
            })->dailyAt('02:10');
82
        }
83
84
        if (config('settings.monthly_report')) {
85
            $schedule->call(function () {
86
                Log::info('Sending monthly hours report');
87
                event(new MonthlyReportEvent());
88
            })->monthlyOn(20);
89
        }
90
91
        $schedule->command('activitylog:clean')->daily();
92
93
        $schedule->command('telescope:prune --hours=96')->daily();
94
    }
95
96
    /**
97
     * Register the commands for the application.
98
     *
99
     * @return void
100
     */
101
    protected function commands()
102
    {
103
        $this->load(__DIR__.'/Commands');
104
105
        require base_path('routes/console.php');
106
    }
107
}
108