refactor -> add task manager

This commit is contained in:
DavidBadura 2015-02-06 12:38:54 +01:00
parent 2709a79986
commit 11c9f610a8
4 changed files with 334 additions and 268 deletions

View File

@ -3,12 +3,12 @@
[![Build Status](https://travis-ci.org/DavidBadura/Taskwarrior.svg?branch=master)](https://travis-ci.org/DavidBadura/Taskwarrior) [![Build Status](https://travis-ci.org/DavidBadura/Taskwarrior.svg?branch=master)](https://travis-ci.org/DavidBadura/Taskwarrior)
```php ```php
$tw = new \DavidBadura\Taskwarrior\Taskwarrior(); $tm = \DavidBadura\Taskwarrior\TaskManager::create();
$task = new \DavidBadura\Taskwarrior\Task(); $task = new \DavidBadura\Taskwarrior\Task();
$task->addTag('home'); $task->addTag('home');
$tw->save($task); $tm->save($task);
$tasks = $tw->filter('+home'); $tasks = $tm->filter('+home');
``` ```

255
src/TaskManager.php Normal file
View File

@ -0,0 +1,255 @@
<?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
{
/**
* @var array
*/
private $taskwarrior;
/**
* @var Task[]
*/
private $tasks = [];
/**
* @param Taskwarrior $taskwarrior
*/
public function __construct(Taskwarrior $taskwarrior)
{
$this->taskwarrior = $taskwarrior;
}
/**
* @param Task $task
*/
public function save(Task $task)
{
if (!$task->getUuid()) {
$this->add($task);
} else {
$this->edit($task);
}
}
/**
* @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()];
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());
$this->update($task);
}
/**
* @param Task $task
*/
public function done(Task $task)
{
if (!$task->getUuid()) {
return;
}
$this->taskwarrior->done($task->getUuid());
$this->update($task);
}
/**
*
*/
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;
$this->update($task);
}
/**
* @param Task $task
*/
private function edit(Task $task)
{
$options = [];
if ($task->getDue()) {
$options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z');
} else {
$options[] = 'due:';
}
$options[] = $task->getDescription();
$this->taskwarrior->command('modify', $task->getUuid(), $options);
$this->update($task);
}
/**
* @param Task $task
*/
private function update(Task $task)
{
$clean = $this->export($task->getUuid())[0];
$this->setValue($task, 'urgency', $clean->getUrgency());
$this->setValue($task, 'status', $clean->getStatus());
}
/**
* @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);
}
/**
* @param Task $task
* @param string $attr
* @param mixed $value
*/
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());
}
}

View File

@ -16,11 +16,6 @@ class Taskwarrior
*/ */
private $rcOptions; private $rcOptions;
/**
* @var Task[]
*/
private $tasks = [];
/** /**
* @param string $taskrc * @param string $taskrc
* @param string $taskData * @param string $taskData
@ -40,110 +35,19 @@ class Taskwarrior
} }
/** /**
* @param Task $task * @param string $uuid
*/ */
public function save(Task $task) public function delete($uuid)
{ {
if (!$task->getUuid()) { $this->command('delete', $uuid);
$this->add($task);
} else {
$this->edit($task);
}
} }
/** /**
* @param string $uuid * @param string $uuid
* @return Task
* @throws TaskwarriorException
*/ */
public function find($uuid) public function done($uuid)
{ {
if (isset($this->tasks[$uuid])) { $this->command('done', $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()];
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->command('delete', $task->getUuid());
$this->update($task);
}
/**
* @param Task $task
*/
public function done(Task $task)
{
if (!$task->getUuid()) {
return;
}
$this->command('done', $task->getUuid());
$this->update($task);
}
/**
*
*/
public function clear()
{
$this->tasks = [];
} }
/** /**
@ -151,7 +55,7 @@ class Taskwarrior
* @return string * @return string
* @throws TaskwarriorException * @throws TaskwarriorException
*/ */
private function import($json) public function import($json)
{ {
$fs = new Filesystem(); $fs = new Filesystem();
@ -169,17 +73,11 @@ class Taskwarrior
/** /**
* @param string|array $filter * @param string|array $filter
* @return Task[] * @return string
*/ */
private function export($filter = null) public function export($filter = null)
{ {
$json = $this->command('export', $filter); return $this->command('export', $filter);
$serializer = SerializerBuilder::create()
->addDefaultHandlers()
->build();
return $serializer->deserialize($json, 'array<DavidBadura\Taskwarrior\Task>', 'json');
} }
/** /**
@ -189,7 +87,7 @@ class Taskwarrior
* @return string * @return string
* @throws TaskwarriorException * @throws TaskwarriorException
*/ */
private function command($command, $filter = null, array $options = array()) public function command($command, $filter = null, array $options = array())
{ {
$builder = $this->createProcessBuilder(); $builder = $this->createProcessBuilder();
@ -225,71 +123,6 @@ class Taskwarrior
return $process->getOutput(); return $process->getOutput();
} }
/**
* @param Task $task
* @throws TaskwarriorException
*/
private function add(Task $task)
{
$json = $this->serializeTask($task);
$uuid = $this->import($json);
$this->setValue($task, 'uuid', $uuid);
$this->tasks[$uuid] = $task;
$this->update($task);
}
/**
* @param Task $task
*/
private function edit(Task $task)
{
$options = [];
if ($task->getDue()) {
$options[] = 'due:' . $task->getDue()->format('Ymd\THis\Z');
} else {
$options[] = 'due:';
}
$options[] = $task->getDescription();
$this->command('modify', $task->getUuid(), $options);
$this->update($task);
}
/**
* @param Task $task
*/
private function update(Task $task)
{
$clean = $this->export($task->getUuid())[0];
$this->setValue($task, 'urgency', $clean->getUrgency());
$this->setValue($task, 'status', $clean->getStatus());
}
/**
* @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;
}
/** /**
* @return ProcessBuilder * @return ProcessBuilder
*/ */
@ -306,33 +139,4 @@ class Taskwarrior
return $builder; return $builder;
} }
/**
*
* @param Task $task
* @return string
*/
private function serializeTask(Task $task)
{
$serializer = SerializerBuilder::create()
->addDefaultHandlers()
->build();
$result = $serializer->serialize($task, 'json');
return str_replace("\\/", "/", $result);
}
/**
* @param Task $task
* @param string $attr
* @param mixed $value
*/
private function setValue(Task $task, $attr, $value)
{
$reflectionClass = new \ReflectionClass('DavidBadura\Taskwarrior\Task');
$prop = $reflectionClass->getProperty($attr);
$prop->setAccessible(true);
$prop->setValue($task, $value);
}
} }

View File

@ -3,22 +3,29 @@
namespace DavidBadura\Taskwarrior\Test; namespace DavidBadura\Taskwarrior\Test;
use DavidBadura\Taskwarrior\Task; use DavidBadura\Taskwarrior\Task;
use DavidBadura\Taskwarrior\TaskManager;
use DavidBadura\Taskwarrior\Taskwarrior; use DavidBadura\Taskwarrior\Taskwarrior;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
/** /**
* @author David Badura <badura@simplethings.de> * @author David Badura <badura@simplethings.de>
*/ */
class TaskwarriorTest extends \PHPUnit_Framework_TestCase class TaskManagerTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @var Taskwarrior * @var Taskwarrior
*/ */
protected $taskwarrior; protected $taskwarrior;
/**
* @var TaskManager
*/
protected $taskManager;
public function setUp() public function setUp()
{ {
$this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task'); $this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task');
$this->taskManager = new TaskManager($this->taskwarrior);
} }
public function tearDown() public function tearDown()
@ -30,7 +37,7 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
public function testEmpty() public function testEmpty()
{ {
$tasks = $this->taskwarrior->filter(); $tasks = $this->taskManager->filter();
$this->assertEmpty($tasks); $this->assertEmpty($tasks);
} }
@ -39,12 +46,12 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task = new Task(); $task = new Task();
$task->setDescription('foo'); $task->setDescription('foo');
$this->taskwarrior->save($task); $this->taskManager->save($task);
$this->taskwarrior->clear(); $this->taskManager->clear();
$this->assertNotEmpty($task->getUuid()); $this->assertNotEmpty($task->getUuid());
$result = $this->taskwarrior->find($task->getUuid()); $result = $this->taskManager->find($task->getUuid());
$this->assertEquals($task, $result); $this->assertEquals($task, $result);
} }
@ -54,9 +61,9 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task = new Task(); $task = new Task();
$task->setDescription('foo'); $task->setDescription('foo');
$this->taskwarrior->save($task); $this->taskManager->save($task);
$result = $this->taskwarrior->find($task->getUuid()); $result = $this->taskManager->find($task->getUuid());
$this->assertSame($task, $result); $this->assertSame($task, $result);
} }
@ -66,16 +73,16 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task = new Task(); $task = new Task();
$task->setDescription('foo'); $task->setDescription('foo');
$this->taskwarrior->save($task); $this->taskManager->save($task);
$result = $this->taskwarrior->filterAll($task->getUuid()); $result = $this->taskManager->filterAll($task->getUuid());
$this->assertSame($task, $result[0]); $this->assertSame($task, $result[0]);
} }
public function testDontFind() public function testDontFind()
{ {
$task = $this->taskwarrior->find('56464asd46s4adas54da6'); $task = $this->taskManager->find('56464asd46s4adas54da6');
$this->assertNull($task); $this->assertNull($task);
} }
@ -84,22 +91,22 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task = new Task(); $task = new Task();
$task->setDescription('foo'); $task->setDescription('foo');
$this->taskwarrior->save($task); $this->taskManager->save($task);
$this->assertNotEmpty($task->getUuid()); $this->assertNotEmpty($task->getUuid());
$uuid = $task->getUuid(); $uuid = $task->getUuid();
$this->taskwarrior->save($task); $this->taskManager->save($task);
$this->assertEquals($uuid, $task->getUuid()); $this->assertEquals($uuid, $task->getUuid());
$this->assertCount(1, $this->taskwarrior->filter()); $this->assertCount(1, $this->taskManager->filter());
$this->taskwarrior->clear(); $this->taskManager->clear();
$this->taskwarrior->save($task); $this->taskManager->save($task);
$this->assertEquals($uuid, $task->getUuid()); $this->assertEquals($uuid, $task->getUuid());
$this->assertCount(1, $this->taskwarrior->filter()); $this->assertCount(1, $this->taskManager->filter());
} }
public function testFilterAll() public function testFilterAll()
@ -110,10 +117,10 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task2 = new Task(); $task2 = new Task();
$task2->setDescription('foo2'); $task2->setDescription('foo2');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->taskwarrior->save($task2); $this->taskManager->save($task2);
$this->assertCount(2, $this->taskwarrior->filter()); $this->assertCount(2, $this->taskManager->filter());
} }
public function testMultiFilter() public function testMultiFilter()
@ -124,10 +131,10 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task2 = new Task(); $task2 = new Task();
$task2->setDescription('foo2'); $task2->setDescription('foo2');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->taskwarrior->save($task2); $this->taskManager->save($task2);
$this->assertCount(2, $this->taskwarrior->filter('status:pending')); // todo better test $this->assertCount(2, $this->taskManager->filter('status:pending')); // todo better test
} }
public function testPending() public function testPending()
@ -135,18 +142,18 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
$this->assertTrue($task1->isPending()); $this->assertTrue($task1->isPending());
$this->taskwarrior->clear(); $this->taskManager->clear();
$result = $this->taskwarrior->find($task1->getUuid()); $result = $this->taskManager->find($task1->getUuid());
$this->assertEquals(Task::STATUS_PENDING, $result->getStatus()); $this->assertEquals(Task::STATUS_PENDING, $result->getStatus());
$this->assertTrue($result->isPending()); $this->assertTrue($result->isPending());
$this->assertCount(1, $this->taskwarrior->filter()); $this->assertCount(1, $this->taskManager->filter());
} }
public function testDelete() public function testDelete()
@ -154,17 +161,17 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$this->assertCount(0, $this->taskwarrior->filter()); $this->assertCount(0, $this->taskManager->filter());
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertCount(1, $this->taskwarrior->filterAll()); $this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(1, $this->taskwarrior->filter()); $this->assertCount(1, $this->taskManager->filter());
$this->assertFalse($task1->isDeleted()); $this->assertFalse($task1->isDeleted());
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
$this->taskwarrior->delete($task1); $this->taskManager->delete($task1);
$this->assertCount(1, $this->taskwarrior->filterAll()); $this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(0, $this->taskwarrior->filter()); $this->assertCount(0, $this->taskManager->filter());
$this->assertTrue($task1->isDeleted()); $this->assertTrue($task1->isDeleted());
$this->assertEquals(Task::STATUS_DELETED, $task1->getStatus()); $this->assertEquals(Task::STATUS_DELETED, $task1->getStatus());
} }
@ -174,17 +181,17 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$this->assertCount(0, $this->taskwarrior->filter()); $this->assertCount(0, $this->taskManager->filter());
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertCount(1, $this->taskwarrior->filterAll()); $this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(1, $this->taskwarrior->filter()); $this->assertCount(1, $this->taskManager->filter());
$this->assertFalse($task1->isCompleted()); $this->assertFalse($task1->isCompleted());
$this->assertEquals(Task::STATUS_PENDING, $task1->getStatus()); $this->assertEquals(Task::STATUS_PENDING, $task1->getStatus());
$this->taskwarrior->done($task1); $this->taskManager->done($task1);
$this->assertCount(1, $this->taskwarrior->filterAll()); $this->assertCount(1, $this->taskManager->filterAll());
$this->assertCount(0, $this->taskwarrior->filter()); $this->assertCount(0, $this->taskManager->filter());
$this->assertTrue($task1->isCompleted()); $this->assertTrue($task1->isCompleted());
$this->assertEquals(Task::STATUS_COMPLETED, $task1->getStatus()); $this->assertEquals(Task::STATUS_COMPLETED, $task1->getStatus());
} }
@ -194,16 +201,16 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertEquals('foo1', $task1->getDescription()); $this->assertEquals('foo1', $task1->getDescription());
$task1->setDescription('bar1'); $task1->setDescription('bar1');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->taskwarrior->clear(); $this->taskManager->clear();
$result = $this->taskwarrior->find($task1->getUuid()); $result = $this->taskManager->find($task1->getUuid());
$this->assertEquals('bar1', $result->getDescription()); $this->assertEquals('bar1', $result->getDescription());
} }
@ -216,28 +223,28 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$task1->setDue($date); $task1->setDue($date);
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->taskwarrior->clear(); $this->taskManager->clear();
$task2 = $this->taskwarrior->find($task1->getUuid()); $task2 = $this->taskManager->find($task1->getUuid());
$this->assertEquals($date, $task2->getDue()); $this->assertEquals($date, $task2->getDue());
$newDate = $this->createDateTime('2002-02-20 11:12:13'); $newDate = $this->createDateTime('2002-02-20 11:12:13');
$task2->setDue($newDate); $task2->setDue($newDate);
$this->taskwarrior->save($task2); $this->taskManager->save($task2);
$this->taskwarrior->clear(); $this->taskManager->clear();
$task3 = $this->taskwarrior->find($task1->getUuid()); $task3 = $this->taskManager->find($task1->getUuid());
$this->assertEquals($newDate, $task3->getDue()); $this->assertEquals($newDate, $task3->getDue());
$task2->setDue(null); $task2->setDue(null);
$this->taskwarrior->save($task2); $this->taskManager->save($task2);
$this->taskwarrior->clear(); $this->taskManager->clear();
$task3 = $this->taskwarrior->find($task1->getUuid()); $task3 = $this->taskManager->find($task1->getUuid());
$this->assertNull($task3->getDue()); $this->assertNull($task3->getDue());
} }
@ -247,13 +254,13 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$task1->setDue($this->createDateTime('1989-01-08 11:12:13')); $task1->setDue($this->createDateTime('1989-01-08 11:12:13'));
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertEquals(12, $task1->getUrgency()); $this->assertEquals(12, $task1->getUrgency());
$task1->setDue(null); $task1->setDue(null);
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->assertEquals(0, $task1->getUrgency()); $this->assertEquals(0, $task1->getUrgency());
} }
@ -270,15 +277,15 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
$task3 = new Task(); $task3 = new Task();
$task3->setDescription('foo3'); $task3->setDescription('foo3');
$this->taskwarrior->save($task1); $this->taskManager->save($task1);
$this->taskwarrior->save($task2); $this->taskManager->save($task2);
$this->taskwarrior->save($task3); $this->taskManager->save($task3);
$this->assertEquals(0, $task1->getUrgency()); $this->assertEquals(0, $task1->getUrgency());
$this->assertEquals(12, $task2->getUrgency()); $this->assertEquals(12, $task2->getUrgency());
$this->assertEquals(0, $task3->getUrgency()); $this->assertEquals(0, $task3->getUrgency());
$tasks = $this->taskwarrior->filter(); $tasks = $this->taskManager->filter();
$this->assertEquals(array($task2, $task1, $task3), $tasks); $this->assertEquals(array($task2, $task1, $task3), $tasks);
} }