-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.php
170 lines (129 loc) · 4.58 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
require_once "taskManager.php";
require_once "magic.php";
// Create our task manager
$taskManager = new TaskManager();
// Ensure we have a action,
if (!isset($argv[1])) {
printHelp();
exit(0);
}
$action = $argv[1];
switch ($action) {
case "add":
$title = getArg($argv, 2, "Missing required parameter 'title' (task-cli add <title>)");
// Add the task
$id = $taskManager->addTask($title);
// Assume okay. Program will crash on failure.
print("Task added successfully (ID: {$id})");
break;
case "list":
$filter = $argv[2] ?? null;
$status = $statusMap[$filter] ?? null;
$tasks = createFilteredTaskList($taskManager, $status);
if (empty($tasks)) {
echo $status === null ? "No tasks found." : "No tasks found for filter '{$filter}'";
break;
}
foreach ($tasks as $task) {
printTask($task, $taskManager);
}
break;
// Fall through both of our cases
case "mark-in-progress":
case "mark-done":
$id = getArg($argv, 2, "Missing required parameter 'id' (task-cli {$action} <id>)");
// Set our status depending on the current action.
$status = $action === "mark-in-progress" ? STATUS_IN_PROGRESS : STATUS_DONE;
if (changeTaskStatus($id, $status, $taskManager)) {
echo "Task updated successfully to " . ($status === STATUS_DONE ? "done" : "in progress") . "!";
break;
}
// Fail state
echo "Failed to update task. Does task {$id} exist?";
break;
case "update":
$id = getArg($argv, 2, "Missing required parameter 'id' (task-cli update <id> <title>)");
$title = getArg($argv, 3, "Missing required parameter 'title' (task-cli update <id> <title>)");
$task = $taskManager->getTaskById($id);
if (!$task) {
echo "Task ID {$id} not found.";
break;
}
$task["title"] = $title;
$taskManager->updateTask($task);
echo "Task updated successfully.";
break;
case "delete":
$id = getArg($argv, 2, "Missing required parameter 'id' (task-cli delete <id>)");
if ($taskManager->deleteTask($id)) {
echo "Task deleted successfully.";
} else {
echo "Failed to delete task. Does task {$id} exist?";
}
break;
case "help":
default:
printHelp();
break;
}
function getArg(array $argv, int $index, string $errorMessage) {
if (!isset($argv[$index])) {
die($errorMessage . "\n");
}
return $argv[$index];
}
/**
* A warpper function for updating tasks
* @param int $id The task ID to update
* @param int $newPriority The new priority of it
* @param TaskManager $taskManager The task manager to use
* @return bool If it succeeded
*/
function changeTaskStatus(int $id, int $newPriority, TaskManager $taskManager):bool {
$task = $taskManager->getTaskById($id);
if (!$task) {
return false;
}
$task["status"] = $newPriority;
return $taskManager->updateTask($task);
}
function createFilteredTaskList(TaskManager $taskManager, $status=null): array {
$tasks = $taskManager->getAllTasks();
if (empty($tasks)) {
return [];
}
if (!isset($status)) {
return $tasks;
}
$tasks = array_filter($tasks, fn($task) => $task["status"] === $status);
return $tasks;
}
function printTask(array $task, TaskManager $taskManager) {
// Create variables
$relative_created = $taskManager->getRealtiveTime($task["createdAt"]);
$relative_modified = $taskManager->getRealtiveTime($task["updatedAt"]);
$status = $taskManager->convertStatusText($task["status"]);
// Print
echo "---------------\n";
echo "{$task["title"]}\n";
echo "[{$task["id"]}] - {$status}\n";
echo "Created: {$relative_created} \n";
echo "Updated: {$relative_modified} \n";
echo "----------------\n";
return;
}
function printHelp(): void {
$help = <<<EOD
Usage:
task-cli [action]
Actions:
add <title> Add a new task with the given title.
delete <id> Delete the task with the specified ID.
update <id> <title> Update the title of the task with the given ID.
mark-in-progress <id> Marks the task id as in progress (1)
mark-done <id> Marks the task id as done (2)
list [done|todo|in-progress] If blank lists all tasks, otherwise, uses the given filter
EOD;
echo $help;
}