2015-02-06 05:38:54 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DavidBadura\Taskwarrior;
|
|
|
|
|
|
|
|
use JMS\Serializer\SerializerBuilder;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author David Badura <d.a.badura@gmail.com>
|
|
|
|
*/
|
|
|
|
class TaskManager
|
|
|
|
{
|
|
|
|
/**
|
2015-02-06 06:14:58 -06:00
|
|
|
* @var Taskwarrior
|
2015-02-06 05:38:54 -06:00
|
|
|
*/
|
|
|
|
private $taskwarrior;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Task[]
|
|
|
|
*/
|
|
|
|
private $tasks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Taskwarrior $taskwarrior
|
|
|
|
*/
|
|
|
|
public function __construct(Taskwarrior $taskwarrior)
|
|
|
|
{
|
|
|
|
$this->taskwarrior = $taskwarrior;
|
|
|
|
}
|
|
|
|
|
2015-02-06 06:14:58 -06:00
|
|
|
/**
|
|
|
|
* @return Taskwarrior
|
|
|
|
*/
|
|
|
|
public function getTaskwarrior()
|
|
|
|
{
|
|
|
|
return $this->taskwarrior;
|
|
|
|
}
|
|
|
|
|
2015-02-06 05:38:54 -06:00
|
|
|
/**
|
|
|
|
* @param Task $task
|
|
|
|
*/
|
|
|
|
public function save(Task $task)
|
|
|
|
{
|
|
|
|
if (!$task->getUuid()) {
|
|
|
|
$this->add($task);
|
|
|
|
} else {
|
|
|
|
$this->edit($task);
|
|
|
|
}
|
2015-02-06 17:22:17 -06:00
|
|
|
|
|
|
|
$this->refresh($task);
|
2015-02-06 05:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uuid
|
|
|
|
* @return Task
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
|
|
|
public function find($uuid)
|
|
|
|
{
|
|
|
|
if (isset($this->tasks[$uuid])) {
|
|
|
|
return $this->tasks[$uuid];
|
|
|
|
}
|
|
|
|
|
|
|
|
$tasks = $this->filterAll($uuid);
|
|
|
|
|
|
|
|
if (count($tasks) == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($tasks) == 1) {
|
|
|
|
return $tasks[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new TaskwarriorException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|array $filter
|
|
|
|
* @return Task[]
|
|
|
|
*/
|
|
|
|
public function filterAll($filter = null)
|
|
|
|
{
|
|
|
|
if (is_string($filter)) {
|
|
|
|
$filter = explode(' ', $filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->export($filter);
|
|
|
|
|
|
|
|
foreach ($result as $key => $task) {
|
|
|
|
if (isset($this->tasks[$task->getUuid()])) {
|
|
|
|
|
|
|
|
$result[$key] = $this->tasks[$task->getUuid()];
|
2015-02-06 17:22:17 -06:00
|
|
|
$this->merge($result[$key], $task);
|
2015-02-06 05:38:54 -06:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->tasks[$task->getUuid()] = $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|array $filter
|
|
|
|
* @return Task[]
|
|
|
|
*/
|
|
|
|
public function filter($filter = null)
|
|
|
|
{
|
|
|
|
$tasks = $this->filterAll($filter . ' status:pending');
|
|
|
|
|
|
|
|
return $this->sort($tasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Task $task
|
|
|
|
*/
|
|
|
|
public function delete(Task $task)
|
|
|
|
{
|
|
|
|
if (!$task->getUuid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->taskwarrior->delete($task->getUuid());
|
2015-02-06 17:22:17 -06:00
|
|
|
$this->refresh($task);
|
2015-02-06 05:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Task $task
|
|
|
|
*/
|
|
|
|
public function done(Task $task)
|
|
|
|
{
|
|
|
|
if (!$task->getUuid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->taskwarrior->done($task->getUuid());
|
2015-02-06 17:22:17 -06:00
|
|
|
$this->refresh($task);
|
2015-02-06 05:38:54 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 06:08:13 -06:00
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param Task $task
|
2015-02-06 06:08:13 -06:00
|
|
|
*/
|
2015-02-06 17:22:17 -06:00
|
|
|
public function refresh(Task $task)
|
2015-02-06 06:08:13 -06:00
|
|
|
{
|
2015-02-06 17:22:17 -06:00
|
|
|
$clean = $this->export($task->getUuid())[0];
|
|
|
|
$this->merge($task, $clean);
|
2015-02-06 06:08:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 05:38:54 -06:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->tasks = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|array $filter
|
|
|
|
* @return Task[]
|
|
|
|
*/
|
|
|
|
private function export($filter = null)
|
|
|
|
{
|
|
|
|
$json = $this->taskwarrior->export($filter);
|
|
|
|
|
|
|
|
$serializer = SerializerBuilder::create()
|
|
|
|
->addDefaultHandlers()
|
|
|
|
->build();
|
|
|
|
|
|
|
|
return $serializer->deserialize($json, 'array<DavidBadura\Taskwarrior\Task>', 'json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Task $task
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
|
|
|
private function add(Task $task)
|
|
|
|
{
|
|
|
|
$json = $this->serializeTask($task);
|
|
|
|
$uuid = $this->taskwarrior->import($json);
|
|
|
|
|
|
|
|
$this->setValue($task, 'uuid', $uuid);
|
|
|
|
$this->tasks[$uuid] = $task;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Task $task
|
|
|
|
*/
|
|
|
|
private function edit(Task $task)
|
|
|
|
{
|
2015-02-06 06:08:13 -06:00
|
|
|
$this->taskwarrior->modify(
|
|
|
|
[
|
|
|
|
'description' => $task->getDescription(),
|
|
|
|
'project' => $task->getProject(),
|
2015-02-06 11:05:29 -06:00
|
|
|
'priority' => $task->getPriority(),
|
2015-02-06 11:31:13 -06:00
|
|
|
'tags' => $task->getTags(),
|
2015-02-06 06:08:13 -06:00
|
|
|
'due' => $task->getDue() ? $task->getDue()->format('Ymd\THis\Z') : null,
|
2015-02-06 17:22:17 -06:00
|
|
|
'wait' => $task->getWait() ? $task->getWait()->format('Ymd\THis\Z') : null,
|
2015-02-06 06:08:13 -06:00
|
|
|
],
|
|
|
|
$task->getUuid()
|
|
|
|
);
|
2015-02-06 05:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param Task $old
|
|
|
|
* @param Task $new
|
2015-02-06 05:38:54 -06:00
|
|
|
*/
|
2015-02-06 17:22:17 -06:00
|
|
|
private function merge(Task $old, Task $new)
|
2015-02-06 05:38:54 -06:00
|
|
|
{
|
2015-02-06 17:22:17 -06:00
|
|
|
$this->setValue($old, 'urgency', $new->getUrgency());
|
|
|
|
$this->setValue($old, 'status', $new->getStatus());
|
|
|
|
$this->setValue($old, 'modified', $new->getModified());
|
|
|
|
$this->setValue($old, 'end', $new->getEnd());
|
2015-02-06 05:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Task[] $tasks
|
|
|
|
* @return Task[]
|
|
|
|
*/
|
|
|
|
private function sort(array $tasks)
|
|
|
|
{
|
|
|
|
usort(
|
|
|
|
$tasks,
|
|
|
|
function (Task $a, Task $b) {
|
|
|
|
if (0 != $diff = $b->getUrgency() - $a->getUrgency()) {
|
|
|
|
return $diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $a->getEntry() >= $b->getEntry() ? 1 : -1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param Task $task
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function serializeTask(Task $task)
|
|
|
|
{
|
|
|
|
$serializer = SerializerBuilder::create()
|
|
|
|
->addDefaultHandlers()
|
|
|
|
->build();
|
|
|
|
|
|
|
|
$result = $serializer->serialize($task, 'json');
|
|
|
|
|
|
|
|
return str_replace("\\/", "/", $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param Task $task
|
2015-02-06 05:38:54 -06:00
|
|
|
* @param string $attr
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param mixed $value
|
2015-02-06 05:38:54 -06:00
|
|
|
*/
|
|
|
|
private function setValue(Task $task, $attr, $value)
|
|
|
|
{
|
|
|
|
$reflectionClass = new \ReflectionClass('DavidBadura\Taskwarrior\Task');
|
|
|
|
$prop = $reflectionClass->getProperty($attr);
|
|
|
|
$prop->setAccessible(true);
|
|
|
|
$prop->setValue($task, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function create()
|
|
|
|
{
|
|
|
|
return new self(new Taskwarrior());
|
|
|
|
}
|
|
|
|
}
|