Where-in-the-World-is-Ben/app/Console/Commands/SendDigest.php

75 lines
2.4 KiB
PHP
Raw Normal View History

2022-11-01 04:07:07 +00:00
<?php
namespace App\Console\Commands;
use App\Mail\Digest;
2022-11-02 17:39:49 +00:00
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
2022-11-01 04:07:07 +00:00
use Illuminate\Support\Facades\Mail;
class SendDigest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2022-11-07 22:33:46 +00:00
protected $signature = 'digest:send {--T|test} {--D|daily} {--W|weekly} {--F|fortnightly} {--M|monthly}';
2022-11-01 04:07:07 +00:00
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a digest of recent updates.';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
2022-11-30 18:47:09 +00:00
{
if (!$this->option('daily') &&
!$this->option('weekly') &&
!$this->option('fortnightly') &&
!$this->option('monthly')
) {
2022-11-01 04:07:07 +00:00
$this->error('No schedule specified.');
return;
}
2022-11-07 22:33:46 +00:00
$test_address = $this->option('test') ? [config('app.test_address')] : null;
2022-11-01 04:07:07 +00:00
// These are seperated because I may want to send multiple types
// of digest in a single commend.
if ($this->option('daily')) {
2022-11-07 22:33:46 +00:00
foreach (($test_address ?? config('app.daily_digest_recipients')) as $recipient) {
2022-11-02 17:39:49 +00:00
Log::debug("Daily digest email sent to '{$recipient}'.");
2022-11-01 04:07:07 +00:00
Mail::to($recipient)->send(new Digest('daily', config('app.current_trip_id')));
}
}
if ($this->option('weekly')) {
2022-12-04 18:06:14 +00:00
foreach (($test_address ?? config('app.weekly_digest_recipients')) as $recipient) {
2022-11-02 17:39:49 +00:00
Log::debug("Weekly digest email sent to '{$recipient}'.");
2022-11-01 04:07:07 +00:00
Mail::to($recipient)->send(new Digest('weekly', config('app.current_trip_id')));
}
}
2022-11-07 22:33:46 +00:00
if ($this->option('fortnightly')) {
2022-12-04 18:06:14 +00:00
foreach (($test_address ?? config('app.fortnightly_digest_recipients')) as $recipient) {
2022-11-07 22:33:46 +00:00
Log::debug("Fortnightly digest email sent to '{$recipient}'.");
Mail::to($recipient)->send(new Digest('fortnightly', config('app.current_trip_id')));
}
}
2022-11-01 04:07:07 +00:00
if ($this->option('monthly')) {
2022-12-04 18:06:14 +00:00
foreach (($test_address ?? config('app.monthly_digest_recipients')) as $recipient) {
2022-11-02 17:39:49 +00:00
Log::debug("Monthly digest email sent to '{$recipient}'.");
2022-11-01 04:07:07 +00:00
Mail::to($recipient)->send(new Digest('monthly', config('app.current_trip_id')));
}
}
}
}