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,61 +2,56 @@
|
|||||||
|
|
||||||
namespace Aerex\TaskwarriorPlugin;
|
namespace Aerex\TaskwarriorPlugin;
|
||||||
|
|
||||||
|
use DavidBadura\Taskwarrior\Taskwarrior;
|
||||||
|
use Aerex\Taskwarrior\TaskwarriorManager;
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
|
|
||||||
public function __construct(){
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $bin;
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $taskrc;
|
||||||
|
|
||||||
public function getTaskwarriorInstance(){
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $taskData;
|
||||||
|
|
||||||
if(!$this->isValidConfiguration()){
|
/**
|
||||||
$invalidConfigurationString = $this->invalidConfigurations();
|
* @var array
|
||||||
|
*/
|
||||||
|
private $rcOptions;
|
||||||
|
|
||||||
$invalidConfigurationMessage = sprintf('The following configurations are invalid %s and' .
|
|
||||||
' the default configurations will be used', $invalidConfigurationString);
|
|
||||||
echo($invalidConfigurationMessage);
|
|
||||||
|
|
||||||
$this->setDefaults();
|
public function __construct($taskrc='~/.taskrc', $taskData='~/.task',$rcOptions = [], $bin='task'){
|
||||||
|
$this->taskrc = $taskrc;
|
||||||
|
$this->bin = $bin;
|
||||||
|
$this->taskDat = $taskData;
|
||||||
|
$this->rcOptions = $rcOptions;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->taskrc = $this->getTaskRC();
|
public function getTaskwarriorInstance(){
|
||||||
$this->taskDataDir = $this->getTaskDataDir();
|
|
||||||
$this->taskBinFile = $this->getTaskBinFile();
|
|
||||||
|
|
||||||
$this->taskwarrior = new $Taskwarrior($this->taskrc,$this->taskdatadir, [], $this->taskbinfile);
|
$this->taskwarrior = new Taskwarrior($this->taskrc,$this->taskdatadir, [], $this->taskbinfile);
|
||||||
}
|
$this->taskwarriorManager = new TaskwarriorManager($this->taskwarrior);
|
||||||
|
|
||||||
|
return $this->taskwarriorManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function getLogger(){
|
public function getLogger(){
|
||||||
return $this->logger;
|
return $this->logger;
|
||||||
}
|
}
|
||||||
public function setLogger($logger){
|
public function setLogger($logger){
|
||||||
$this->logger = $logger;
|
$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;
|
namespace Aerex\TaskwarriorPlugin;
|
||||||
|
|
||||||
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
||||||
|
use DavidBadura\Taskwarrior\TaskManager;
|
||||||
|
use DavidBadura\Taskwarrior\Task;
|
||||||
|
|
||||||
class Taskwarrior {
|
class TaskwarriorManager extends TaskManager {
|
||||||
|
|
||||||
const DESCRIPTION = 'description';
|
const DESCRIPTION = 'description';
|
||||||
const CATEGORIES = 'categories';
|
const CATEGORIES = 'categories';
|
||||||
|
const TASK_UUID = 'uuid';
|
||||||
|
const ICAL_UID = 'uid';
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var \DavidBadura\Taskwarrior\Task
|
||||||
|
*/
|
||||||
|
private $tasks;
|
||||||
|
|
||||||
function __constructor(TaskwarriorConfig $taskConfig){
|
const ENTRY = 'entry';
|
||||||
$this->taskConfig = $taskConfig;
|
const START = 'start';
|
||||||
$this->taskWarriorJSON = array();
|
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){
|
function setEntryTime($entry){
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,7 +66,10 @@ class Taskwarrior {
|
|||||||
|
|
||||||
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
|
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
|
||||||
}
|
}
|
||||||
private function taskExists($taskUuid){
|
|
||||||
|
|
||||||
|
|
||||||
|
public function taskExists($taskUuid){
|
||||||
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
|
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
|
||||||
|
|
||||||
if($taskIsInCache){
|
if($taskIsInCache){
|
||||||
@ -58,12 +86,26 @@ class Taskwarrior {
|
|||||||
public function save(){
|
public function save(){
|
||||||
|
|
||||||
}
|
}
|
||||||
public function setUUID(){
|
|
||||||
}
|
|
||||||
public function build(){
|
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 setDescription($description){}
|
||||||
|
function parseiCalDateTime($iCalDateTime){}
|
||||||
|
function convertToStringArray($categories){}
|
||||||
function setDueDate($dueDate){}
|
function setDueDate($dueDate){}
|
||||||
}
|
}
|
||||||
|
|
@ -69,24 +69,35 @@ class iCalEventProcessor {
|
|||||||
|
|
||||||
if($this->taskwarrior->exists($ToDoComponent->UID)){
|
if($this->taskwarrior->exists($ToDoComponent->UID)){
|
||||||
$this->logger->error("Event already exists " . (string)$ToDoComponent->UID);
|
$this->logger->error("Event already exists " . (string)$ToDoComponent->UID);
|
||||||
throw new Exception\BadRequest("Event already exists " . $ToDoComponent->UID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
try {
|
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){
|
} catch(Exception $e){
|
||||||
$this->logger->error($e->message);
|
$this->logger->error($e->message);
|
||||||
|
@ -3,9 +3,10 @@
|
|||||||
use Sabre\DAV\Exception\BadRequest;
|
use Sabre\DAV\Exception\BadRequest;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Sabre\VObject\Component\VCalendar;
|
use Sabre\VObject\Component\VCalendar;
|
||||||
use Aerex\TaskwarriorPlugin\Taskwarrior;
|
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
|
||||||
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
|
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
|
||||||
use Aerex\TaskwarriorPlugin\Config;
|
use Aerex\TaskwarriorPlugin\Config;
|
||||||
|
use DavidBadura\Taskwarrior\Task;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
|
|
||||||
@ -23,13 +24,14 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
private $mockTaskwarriorConfig;
|
private $mockTaskwarriorConfig;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* @var \PHPUnit_Framework_MockObject_MockObject
|
* @var \PHPUnit_Framework__MockObject
|
||||||
*/
|
*/
|
||||||
private $mockLogger;
|
private $mockLogger;
|
||||||
|
|
||||||
function setup(){
|
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->mockTaskwarriorConfig = $this->createMock(Config::class);
|
||||||
$this->mockLogger = $this->createMock(Logger::class);
|
$this->mockLogger = $this->createMock(Logger::class);
|
||||||
|
|
||||||
@ -62,34 +64,30 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
$mockVTodo->add('DESCRIPTION', $description);
|
$mockVTodo->add('DESCRIPTION', $description);
|
||||||
$mockVTodo->add('CATEGORIES', $categories);
|
$mockVTodo->add('CATEGORIES', $categories);
|
||||||
|
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setUUID')
|
$this->mockTaskwarrior->expects($this->exactly(4))
|
||||||
->with($this->equalTo($mockVTodo->UID));
|
->method('parseiCalDateTime')
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setEntryTime')
|
->will($this->onConsecutiveCalls($startTime, $startTime, $modifiedTime, $endTime));
|
||||||
->with($this->equalTo($mockVTodo->DSTAMP));
|
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setEndTime')
|
$this->mockTaskwarrior->expects($this->once())->method('createTask')->with($this->equalTo($uuid))
|
||||||
->with($this->equalTo($mockVTodo->DTEND));
|
->willReturn($this->mockTask);
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setDueDate')
|
$this->mockTask->expects($this->once())->method('setDue')->with($this->equalTo($dueDate));
|
||||||
->with($this->equalTo($mockVTodo->DUE));
|
$this->mockTask->expects($this->once())->method('setDescription')->with($this->equalTo($description));
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setSummary')
|
$this->mockTaskwarrior->expects($this->once())->method('convertToStringArray')->with($this->equalTo($mockVTodo->CATEGORIES))
|
||||||
->with($this->equalTo($mockVTodo->DESCRIPTION));
|
->willReturn($categories);
|
||||||
$this->mockTaskwarrior->expects($this->once())->method('setCategories')
|
|
||||||
->with($this->equalTo($mockVTodo->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->mockLogger->expects($this->never())->method('error');
|
||||||
|
|
||||||
$this
|
$this->mockTaskwarriorConfig
|
||||||
->mockTaskwarriorConfig
|
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('getTaskwarriorInstance')
|
->method('getTaskwarriorInstance')
|
||||||
->will($this->returnValue($this->mockTaskwarrior));
|
->will($this->returnValue($this->mockTaskwarrior));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$this
|
|
||||||
->mockTaskwarrior
|
|
||||||
->expects($this->once())
|
|
||||||
->method('build');
|
|
||||||
|
|
||||||
$this
|
$this
|
||||||
->mockTaskwarrior
|
->mockTaskwarrior
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
@ -106,7 +104,7 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
*/
|
*/
|
||||||
function testFailTaskNoComponentDefine(){
|
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('setEntryTime');
|
||||||
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
||||||
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
||||||
@ -129,11 +127,14 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException Sabre\DAV\Exception\BadRequest
|
* @expectedException Sabre\DAV\Exception\BadRequest
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
function testFailTaskExists(){
|
function testFailTaskExists(){
|
||||||
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
|
$uuid = '9f353281-1051-4c45-92db-462f5d353c76';
|
||||||
|
|
||||||
@ -141,7 +142,7 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
$mockVTodo = $mockVCalendar->add('VTODO', ['UID' => $uuid]);
|
||||||
$expectedErrorMessage = "already exists";
|
$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('setEntryTime');
|
||||||
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
$this->mockTaskwarrior->expects($this->never())->method('setEndTime');
|
||||||
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
$this->mockTaskwarrior->expects($this->never())->method('setDueDate');
|
||||||
@ -165,6 +166,8 @@ class iCalEventProcessorTest extends \PHPUnit\Framework\TestCase {
|
|||||||
$twCalEvent->importTask($mockVTodo);
|
$twCalEvent->importTask($mockVTodo);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user