Where-in-the-World-is-Ben/app/Mail/Digest.php

98 lines
2.2 KiB
PHP
Raw Normal View History

2022-11-01 04:07:07 +00:00
<?php
namespace App\Mail;
use DateTime;
use App\Http\Controllers\TrackerController;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class Digest extends Mailable
{
use Queueable, SerializesModels;
/**
* The type of digest (e.g. weekly, monthly, etc.).
* TODO: Replace with enum
*
* @var string
*/
public $digest_type;
/**
* The current trip locations as a JSON object.
*
2024-06-09 20:29:04 +00:00
* @var obj
2022-11-01 04:07:07 +00:00
*/
public $locations;
/**
* The current trip checkins as a JSON object.
*
2024-06-09 20:29:04 +00:00
* @var obj
2022-11-01 04:07:07 +00:00
*/
public $checkinsList;
2022-11-01 04:07:07 +00:00
2024-06-09 20:29:04 +00:00
/**
* The current trip object.
*
* @var obj
*/
public $trip;
2022-11-01 04:07:07 +00:00
/**
* Create a new message instance.
*
* @param string $digest_type
* @param string $trip_id
* @return void
*/
public function __construct(string $digest_type, string $trip_id)
{
$this->digest_type = $digest_type;
2024-06-09 20:29:04 +00:00
$this->trip = (new TrackerController)->get_trip_data($trip_id);
2022-11-01 04:07:07 +00:00
$cutoffDateTime = new DateTime();
switch ($this->digest_type) {
2022-11-30 18:47:09 +00:00
case 'daily':
$cutoffDateTime->modify('-1 day');
break;
case 'weekly':
$cutoffDateTime->modify('-1 week');
break;
case 'fortnightly':
$cutoffDateTime->modify('-2 weeks');
break;
case 'monthly':
$cutoffDateTime->modify('-1 month');
break;
default:
2022-11-01 04:07:07 +00:00
}
$this->checkinsList = array_filter(
2024-06-09 20:29:04 +00:00
$this->trip->checkins,
2022-11-30 18:47:09 +00:00
function ($elem) use ($cutoffDateTime) {
2022-11-01 04:07:07 +00:00
$elemDateTime = new DateTime($elem->created_at);
return $elemDateTime > $cutoffDateTime;
}
);
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
2024-06-09 20:29:04 +00:00
return $this->view(
'emails.digest',
[
'trip' => $this->trip
]
)->subject("track.bengoldsworthy.net ".ucwords($this->digest_type)." Digest");
2022-11-01 04:07:07 +00:00
}
}