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