97 lines
2.2 KiB
PHP
97 lines
2.2 KiB
PHP
<?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.
|
|
*
|
|
* @var obj
|
|
*/
|
|
public $locations;
|
|
|
|
/**
|
|
* The current trip checkins as a JSON object.
|
|
*
|
|
* @var obj
|
|
*/
|
|
public $checkinsList;
|
|
|
|
/**
|
|
* The current trip object.
|
|
*
|
|
* @var obj
|
|
*/
|
|
public $trip;
|
|
|
|
/**
|
|
* 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;
|
|
$this->trip = (new TrackerController)->get_trip_data($trip_id);
|
|
|
|
$cutoffDateTime = new DateTime();
|
|
switch ($this->digest_type) {
|
|
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:
|
|
}
|
|
|
|
$this->checkinsList = array_filter(
|
|
$this->trip->checkins,
|
|
function ($elem) use ($cutoffDateTime) {
|
|
$elemDateTime = new DateTime($elem->created_at);
|
|
return $elemDateTime > $cutoffDateTime;
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Build the message.
|
|
*
|
|
* @return $this
|
|
*/
|
|
public function build()
|
|
{
|
|
return $this->view(
|
|
'emails.digest',
|
|
[
|
|
'trip' => $this->trip
|
|
]
|
|
)->subject("track.bengoldsworthy.net ".ucwords($this->digest_type)." Digest");
|
|
}
|
|
}
|