refactor: leverage Task and TaskManager external api methods to create task
refactor: remove uneeded configuration
This commit is contained in:
parent
5201db7d22
commit
88a78aeae7
@ -2,31 +2,46 @@
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin;
|
||||
|
||||
|
||||
use DavidBadura\Taskwarrior\Taskwarrior;
|
||||
use Aerex\Taskwarrior\TaskwarriorManager;
|
||||
|
||||
class Config {
|
||||
|
||||
public function __construct(){
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $bin;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $taskrc;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $taskData;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $rcOptions;
|
||||
|
||||
|
||||
public function __construct($taskrc='~/.taskrc', $taskData='~/.task',$rcOptions = [], $bin='task'){
|
||||
$this->taskrc = $taskrc;
|
||||
$this->bin = $bin;
|
||||
$this->taskDat = $taskData;
|
||||
$this->rcOptions = $rcOptions;
|
||||
|
||||
}
|
||||
|
||||
public function getTaskwarriorInstance(){
|
||||
|
||||
if(!$this->isValidConfiguration()){
|
||||
$invalidConfigurationString = $this->invalidConfigurations();
|
||||
$this->taskwarrior = new Taskwarrior($this->taskrc,$this->taskdatadir, [], $this->taskbinfile);
|
||||
$this->taskwarriorManager = new TaskwarriorManager($this->taskwarrior);
|
||||
|
||||
$invalidConfigurationMessage = sprintf('The following configurations are invalid %s and' .
|
||||
' the default configurations will be used', $invalidConfigurationString);
|
||||
echo($invalidConfigurationMessage);
|
||||
|
||||
$this->setDefaults();
|
||||
}
|
||||
|
||||
$this->taskrc = $this->getTaskRC();
|
||||
$this->taskDataDir = $this->getTaskDataDir();
|
||||
$this->taskBinFile = $this->getTaskBinFile();
|
||||
|
||||
$this->taskwarrior = new $Taskwarrior($this->taskrc,$this->taskdatadir, [], $this->taskbinfile);
|
||||
return $this->taskwarriorManager;
|
||||
}
|
||||
|
||||
|
||||
@ -37,26 +52,6 @@ class Config {
|
||||
$this->logger = $logger;
|
||||
|
||||
}
|
||||
public function isValidConfigurations(){
|
||||
|
||||
}
|
||||
|
||||
public function setDefaults(){
|
||||
|
||||
}
|
||||
|
||||
public function getTaskRC() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function getTaskDataDir(){
|
||||
|
||||
}
|
||||
|
||||
public function getTaskBinFile(){
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
@ -3,18 +3,43 @@
|
||||
namespace Aerex\TaskwarriorPlugin;
|
||||
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
||||
use DavidBadura\Taskwarrior\TaskManager;
|
||||
use DavidBadura\Taskwarrior\Task;
|
||||
|
||||
class Taskwarrior {
|
||||
class TaskwarriorManager extends TaskManager {
|
||||
|
||||
const DESCRIPTION = 'description';
|
||||
const CATEGORIES = 'categories';
|
||||
const TASK_UUID = 'uuid';
|
||||
const ICAL_UID = 'uid';
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \DavidBadura\Taskwarrior\Task
|
||||
*/
|
||||
private $tasks;
|
||||
|
||||
function __constructor(TaskwarriorConfig $taskConfig){
|
||||
$this->taskConfig = $taskConfig;
|
||||
$this->taskWarriorJSON = array();
|
||||
const ENTRY = 'entry';
|
||||
const START = 'start';
|
||||
const MODIFIED = 'modified';
|
||||
const END = 'end';
|
||||
|
||||
public function __construct($taskwarrior){
|
||||
parent::__construct($taskwarrior);
|
||||
}
|
||||
|
||||
public function createTask($UUID){
|
||||
$task = new Task();
|
||||
$this->setValue($task, $ICAL_UID, $UUID);
|
||||
return $task;
|
||||
}
|
||||
|
||||
public function export(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
function setEntryTime($entry){
|
||||
}
|
||||
|
||||
@ -41,7 +66,10 @@ class Taskwarrior {
|
||||
|
||||
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
|
||||
}
|
||||
private function taskExists($taskUuid){
|
||||
|
||||
|
||||
|
||||
public function taskExists($taskUuid){
|
||||
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
|
||||
|
||||
if($taskIsInCache){
|
||||
@ -58,12 +86,26 @@ class Taskwarrior {
|
||||
public function save(){
|
||||
|
||||
}
|
||||
public function setUUID(){
|
||||
}
|
||||
public function build(){
|
||||
|
||||
}
|
||||
/**
|
||||
* @param Task $task
|
||||
* @param string $attr
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setValue(Task $task, $attr, $value)
|
||||
{
|
||||
$refClass = new \ReflectionClass(Task::class);
|
||||
$refProp = $refClass->getProperty($attr);
|
||||
$refProp->setAccessible(true);
|
||||
$refProp->setValue($task, $value);
|
||||
}
|
||||
|
||||
|
||||
function setDescription($description){}
|
||||
function parseiCalDateTime($iCalDateTime){}
|
||||
function convertToStringArray($categories){}
|
||||
function setDueDate($dueDate){}
|
||||
}
|
||||
|
@ -69,24 +69,35 @@ class iCalEventProcessor {
|
||||
|
||||
if($this->taskwarrior->exists($ToDoComponent->UID)){
|
||||
$this->logger->error("Event already exists " . (string)$ToDoComponent->UID);
|
||||
throw new Exception\BadRequest("Event already exists " . $ToDoComponent->UID);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
$this->taskwarrior->setUUID($ToDoComponent->UID);
|
||||
$this->taskwarrior->setEntryTime($ToDoComponent->DSTAMP);
|
||||
$this->taskwarrior->setDueDate($ToDoComponent->DUE);
|
||||
$this->taskwarrior->setModifiedTime($ToDoComponent->{'LAST-MODIFIED'});
|
||||
$this->taskwarrior->setStartTime($ToDoComponent->DSTART);
|
||||
$this->taskwarrior->setEndTime($ToDoComponent->DTEND);
|
||||
$this->taskwarrior->setSummary($ToDoComponent->DESCRIPTION);
|
||||
$this->taskwarrior->setCategories($ToDoComponent->CATEGORIES);
|
||||
$this->taskwarrior->setDescription($ToDoComponent->DESCRIPTION);
|
||||
|
||||
$this->taskwarrior->build();
|
||||
|
||||
$this->taskwarrior->save();
|
||||
// parse iCalendar event times to DateTime objects
|
||||
$entry = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DSTAMP);
|
||||
$start = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DSTART);
|
||||
$modified = $this->taskwarrior->parseiCalDateTime($ToDoComponent->{'LAST-MODIFIED'});
|
||||
$end = $this->taskwarrior->parseiCalDateTime($ToDoComponent->DTEND);
|
||||
|
||||
$task = $this->taskwarrior->createTask($ToDoComponent->UID);
|
||||
|
||||
$tags = $this->taskwarrior->convertToStringArray($ToDoComponent->CATEGORIES);
|
||||
$task->setTags($tags);
|
||||
|
||||
$task->setDue($ToDoComponent->DUE->getDateTime());
|
||||
$task->setDescription($ToDoComponent->DESCRIPTION);
|
||||
|
||||
|
||||
// override protected taskwarrior properties using iCal event
|
||||
|
||||
$this->taskwarrior->setValue($task, $this->taskwarrior::ENTRY, $entry);
|
||||
$this->taskwarrior->setValue($task, $this->taskwarrior::START, $start);
|
||||
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
|
||||
$this->taskwarrior->setValue($task, $this->taskwarrior::END, $end);
|
||||
|
||||
$this->taskwarrior->save($task);
|
||||
|
||||
} catch(Exception $e){
|
||||
$this->logger->error($e->message);
|
||||
|
@ -3,9 +3,10 @@
|
||||
use Sabre\DAV\Exception\BadRequest;
|
||||
use Monolog\Logger;
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
||||
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
|
||||
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
use DavidBadura\Taskwarrior\Task;
|
||||
use DateTime;
|
||||
|
||||
|
||||
@ -23,13 +24,14 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
private $mockTaskwarriorConfig;
|
||||
|
||||
/***
|
||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
||||
* @var \PHPUnit_Framework__MockObject
|
||||
*/
|
||||
private $mockLogger;
|
||||
|
||||
function setup(){
|
||||
|
||||
$this->mockTaskwarrior = $this->createMock(Taskwarrior::class);
|
||||
$this->mockTaskwarrior = $this->createMock(TaskwarriorManager::class);
|
||||
$this->mockTask = $this->createMock(Task::class);
|
||||
$this->mockTaskwarriorConfig = $this->createMock(Config::class);
|
||||
$this->mockLogger = $this->createMock(Logger::class);
|
||||
|
||||
@ -62,34 +64,30 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
$mockVTodo->add('DESCRIPTION', $description);
|
||||
$mockVTodo->add('CATEGORIES', $categories);
|
||||
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setUUID')
|
||||
->with($this->equalTo($mockVTodo->UID));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setEntryTime')
|
||||
->with($this->equalTo($mockVTodo->DSTAMP));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setEndTime')
|
||||
->with($this->equalTo($mockVTodo->DTEND));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setDueDate')
|
||||
->with($this->equalTo($mockVTodo->DUE));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setSummary')
|
||||
->with($this->equalTo($mockVTodo->DESCRIPTION));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('setCategories')
|
||||
->with($this->equalTo($mockVTodo->CATEGORIES));
|
||||
$this->mockTaskwarrior->expects($this->exactly(4))
|
||||
->method('parseiCalDateTime')
|
||||
->will($this->onConsecutiveCalls($startTime, $startTime, $modifiedTime, $endTime));
|
||||
|
||||
$this->mockTaskwarrior->expects($this->once())->method('createTask')->with($this->equalTo($uuid))
|
||||
->willReturn($this->mockTask);
|
||||
$this->mockTask->expects($this->once())->method('setDue')->with($this->equalTo($dueDate));
|
||||
$this->mockTask->expects($this->once())->method('setDescription')->with($this->equalTo($description));
|
||||
$this->mockTaskwarrior->expects($this->once())->method('convertToStringArray')->with($this->equalTo($mockVTodo->CATEGORIES))
|
||||
->willReturn($categories);
|
||||
|
||||
$this->mockTask->expects($this->once())->method('setTags')
|
||||
->with($this->equalTo($categories));
|
||||
|
||||
$this->mockTaskwarrior->expects($this->exactly(4))
|
||||
->method('setValue');
|
||||
|
||||
$this->mockLogger->expects($this->never())->method('error');
|
||||
|
||||
$this
|
||||
->mockTaskwarriorConfig
|
||||
$this->mockTaskwarriorConfig
|
||||
->expects($this->once())
|
||||
->method('getTaskwarriorInstance')
|
||||
->will($this->returnValue($this->mockTaskwarrior));
|
||||
|
||||
|
||||
|
||||
$this
|
||||
->mockTaskwarrior
|
||||
->expects($this->once())
|
||||
->method('build');
|
||||
|
||||
$this
|
||||
->mockTaskwarrior
|
||||
->expects($this->once())
|
||||
@ -106,7 +104,7 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
*/
|
||||
function testFailTaskNoComponentDefine(){
|
||||
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setUUID');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('createTask');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
||||
@ -129,11 +127,14 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @expectedException Sabre\DAV\Exception\BadRequest
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
function testFailTaskExists(){
|
||||
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
|
||||
|
||||
@ -141,7 +142,7 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||
$expectedErrorMessage = "already exists";
|
||||
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setUUID');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('createTask');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setEntryTime');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
||||
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
||||
@ -165,6 +166,8 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
||||
$twCalEvent->importTask($mockVTodo);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user