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

95 lines
2.3 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 string
*/
public $locations;
/**
* The current trip checkins as a JSON object.
*
* @var string
*/
public $checkinsList;
/**
* 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;
$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->locations = array_filter(
$trip->locations,
function ($elem) use ($cutoffDateTime) {
$elemDateTime = new DateTime($elem->created_at);
return $elemDateTime > $cutoffDateTime;
}
);
$this->checkinsList = array_filter(
$trip->checkinsList,
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')
->subject("track.bengoldsworthy.net ".ucwords($this->digest_type)." Digest");
}
}