Introduce new Monday type and turnBackTime function to simplify working with Mondays
[SonOfLokstallBot.git] / src / purjolok.php
index 9f4cacd..dcc2eac 100644 (file)
@@ -16,23 +16,13 @@ if(getTelegram()->getWebHookUpdates()->get('message') && !canChatWith(getTelegra
 $message = getTelegram()->getWebHookUpdates()->get('message') ? getTelegram()->getWebHookUpdates()->get('message')->get('text') : '';
 
 if ($between = between(reveal($message ?? ''), '[taskid]')) {
-    // 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);
-
-    $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+    $monday = turnBackTime(new DateTimeImmutable());
+    $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
     $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
     $tasksForTheWeek = getTasksForTheWeek(
-        $yearForThisWeek,
-        $monthForThisWeek,
-        $weekNum,
+        $monday->year,
+        $monday->month,
+        $monday->weekNum,
         require 'taskMatrix.php'
     );
 
@@ -235,25 +225,15 @@ getTelegram()->addCommand(
         protected $description = 'List tasks for this week';
 
         public function handle($arguments) {
-            // 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);
-
+            $monday = turnBackTime(new DateTimeImmutable());
             $tasksForTheWeek = getTasksForTheWeek(
-                $yearForThisWeek,
-                $monthForThisWeek,
-                $weekNum,
+                $monday->year,
+                $monday->month,
+                $monday->weekNum,
                 require 'taskMatrix.php'
             );
 
-            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+            $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
             if (!array_diff($tasksForTheWeek, $completedTasks)) {
@@ -279,25 +259,15 @@ getTelegram()->addCommand(
         protected $description = 'Mark a task as completed';
 
         public function handle($arguments) {
-            // 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);
-
+            $monday = turnBackTime(new DateTimeImmutable);
             $tasksForTheWeek = getTasksForTheWeek(
-                $yearForThisWeek,
-                $monthForThisWeek,
-                $weekNum,
+                $monday->year,
+                $monday->month,
+                $monday->weekNum,
                 require 'taskMatrix.php'
             );
 
-            $completedTasksFile = getFilePathForWeek($yearForThisWeek, $monthForThisWeek, $weekNum);
+            $completedTasksFile = getFilePathForWeek($monday->year, $monday->month, $monday->weekNum);
             $completedTasks = file_exists($completedTasksFile) ? lines(trim(file_get_contents($completedTasksFile))) : [];
 
             if (!array_diff($tasksForTheWeek, $completedTasks)) {