Introduce new Monday type and turnBackTime function to simplify working with Mondays
[SonOfLokstallBot.git] / src / tasks.php
index 38ecb83..42c0f7c 100644 (file)
@@ -3,34 +3,23 @@
 require_once('common.php');
 
 $taskMatrix = require 'taskMatrix.php';
-
-// The actual date/time when the script is called
-$currentYear = (int)(new DateTimeImmutable())->format('Y');
-$currentMonth = (int)(new DateTimeImmutable())->format('n');
-$currentDay = (int)(new DateTimeImmutable())->format('d');
-
-// The years/months/weeks that we are "rewinding" to, for calculations
-$yearForThisWeek = getYearWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$monthForThisWeek = getMonthWeekBeginsIn($currentYear, $currentMonth, $currentDay);
-$seasonForThisWeek = getSeason($monthForThisWeek);
-$weekNum = getWeekNumber($currentYear, $currentMonth, $currentDay);
-$dayNum = getDayNumber($currentYear, $currentMonth, $currentDay);
+$monday = turnBackTime(new DateTimeImmutable());
 
 $taskLists = array_merge(
-    isStartOfSeason($monthForThisWeek, $dayNum) ? [unlines(map(getString)(getTasksForTheSeason($seasonForThisWeek, $taskMatrix)))] : [],
-    isStartOfMonth($dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))] : [],
-    [unlines(map(getString)(getTasksForTheWeek($yearForThisWeek, $monthForThisWeek, $weekNum, $taskMatrix)))]
+    isStartOfSeason($monday->month, $monday->dayNum) ? [unlines(map(getString)(getTasksForTheSeason($monday->season, $taskMatrix)))] : [],
+    isStartOfMonth($monday->dayNum) ? [unlines(map(getString)(getTasksForTheMonth($monday->month, $taskMatrix)))] : [],
+    [unlines(map(getString)(getTasksForTheWeek($monday->year, $monday->month, $monday->weekNum, $taskMatrix)))]
 );
 
 $taskMessages = [
     [getString('happyMonday')],
     [
-        [getString('beginningOf'. ucfirst(getMonthName($monthForThisWeek))), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
+        [getString('beginningOf'. ucfirst(getMonthName($monday->month))), getString('thisMonthThereAre', count(getTasksForTheMonth($monday->month, $taskMatrix)))],
          getString('anywayHeresTheWeek')
     ],
     [
-        getString('beginningOf' . ucfirst(getSeason($monthForThisWeek))),
-        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monthForThisWeek, $taskMatrix)))],
+        getString('beginningOf' . ucfirst($monday->season)),
+        [getString('anywayHeresTheMonth'), getString('thisMonthThereAre', count(getTasksForTheMonth($monday->month, $taskMatrix)))],
         getString('finallyHeresTheWeek')
     ]
 ];
@@ -46,7 +35,7 @@ $messages = zipWith(
     },
     // Magic. startOfSeason implies startofMonth so we get 2, start of month without start of season gives 1 and
     // a regular week (not the start of a season or month) gives 0. And this is how the indicies are ordered in the array.
-    $taskMessages[(int)isStartOfSeason($monthForThisWeek, $dayNum) + (int)isStartOfMonth($dayNum)],
+    $taskMessages[(int)isStartOfSeason($monday->month, $monday->dayNum) + (int)isStartOfMonth($monday->dayNum)],
     $taskLists
 );