add notification flag, fix syntax errors

This commit is contained in:
Jon Goldberg 2020-05-08 20:04:15 -04:00
parent 7726384a2c
commit c027e56dd0
No known key found for this signature in database
GPG Key ID: C2D2247364F9DB13

View File

@ -9,6 +9,9 @@ use Yasumi\Holiday;
$oncall = new oncall(); $oncall = new oncall();
$oncall->dateAndTimeCheck(); $oncall->dateAndTimeCheck();
$oncall->retrieveItems(); $oncall->retrieveItems();
if (!$this->notify) {
exit(0);
}
$oncall->setEmail(); $oncall->setEmail();
$table = $oncall->buildMattermostTable(); $table = $oncall->buildMattermostTable();
if ($table) { if ($table) {
@ -64,6 +67,12 @@ class oncall {
*/ */
private $email; private $email;
/**
* A flag of whether Mattermost should be contacted.
* @var bool
*/
private $notify;
public function __construct() { public function __construct() {
// Load the .env file // Load the .env file
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
@ -82,9 +91,10 @@ class oncall {
/** /**
* Add the number of minutes since a ticket was created. * Add the number of minutes since a ticket was created.
* Also set the everyone flag if necessary. * Also set the everyone and notify flags if necessary.
* @return int
*/ */
private function getMinutes(array $item) { private function calculateMinutes(array $item) {
// find the time since the ticket was created // find the time since the ticket was created
$created = $item['created_on']; $created = $item['created_on'];
$time = strtotime("$created"); $time = strtotime("$created");
@ -93,13 +103,14 @@ class oncall {
// put it into minutes // put it into minutes
$td_min = (int) ($time_difference / 60); $td_min = (int) ($time_difference / 60);
// Store the time in minutes in the array.
$item['minutes'] = $td_min;
// Flag if any items are overdue. // Flag if any items are overdue.
if ($td_min > 60) { if ($td_min > 60) {
$this->everyone = TRUE; $this->everyone = TRUE;
} }
return $item; if ($td_min < 3 || ($td_min >= 30 || $td_min < 33)) {
$this->notify = TRUE;
}
return $td_min;
} }
/** /**
@ -113,7 +124,7 @@ class oncall {
if (!empty($json)) { if (!empty($json)) {
// go through this loop for each ticket found // go through this loop for each ticket found
foreach ($json['issues'] as $k => $item) { foreach ($json['issues'] as $k => $item) {
$item = $this->getMinutes($item); $item['minutes'] = $this->calculateMinutes($item);
$this->queryResults[$title][] = $item; $this->queryResults[$title][] = $item;
} }
} }
@ -195,14 +206,13 @@ class oncall {
$this->email = trim(file_get_contents("$contactfile")); $this->email = trim(file_get_contents("$contactfile"));
} }
/** /**
* Checks whether we're responsible for on-call on this date and time. * Checks whether we're responsible for on-call on this date and time.
*/ */
public function dateAndTimeCheck() { public function dateAndTimeCheck() {
$today = new DateTime(); $today = new DateTime();
$thisYear = (int) $today->format('Y'); $thisYear = (int) $today->format('Y');
$thisHour = (int) $today->formay('H'); $thisHour = (int) $today->format('H');
// Time check. // Time check.
if ($thisHour < 10 || $thisHour > 20) { if ($thisHour < 10 || $thisHour > 20) {
@ -223,12 +233,11 @@ class oncall {
if ($holidays->isHoliday($today)) { if ($holidays->isHoliday($today)) {
$this->everyone = TRUE; $this->everyone = TRUE;
$todaysHoliday = $holidays->on($today); $todaysHoliday = $holidays->on($today);
print_r($todaysHoliday); $holidayName = $todaysHoliday->getName();
die; $this->sendToMattermost("Happy $holidayName Megaphone Tech! There is no on-call today.");
$this->sendToMattermost("Happy $holidayName Megaphone Tech! There is no on-call today.")
} }
// Don't do anything else if it's a holiday or weekend. // Don't do anything else if it's a holiday or weekend.
if (!$holidays->isWorkingDay($today))) { if (!$holidays->isWorkingDay($today)) {
exit(0); exit(0);
} }
} }