refactor: extract ical processor code into an interface type

This commit is contained in:
Aerex 2018-11-05 00:18:47 -06:00
parent 84e96940ed
commit 2d2122e05d
6 changed files with 117 additions and 23 deletions

View File

@ -3,7 +3,7 @@
namespace Aerex\TaskwarriorPlugin; namespace Aerex\TaskwarriorPlugin;
use DavidBadura\Taskwarrior\Taskwarrior; use DavidBadura\Taskwarrior\Taskwarrior;
use Aerex\Taskwarrior\TaskwarriorManager; use Aerex\TaskwarriorPlugin\TaskwarriorManager;
class Config { class Config {

View File

@ -0,0 +1,15 @@
<?php
<?php
namespace ;
/**
* Interface CalendarComponent
* @author Aerex
*/
interface CalendarComponentInterface
{
public function import();
public function export();
?>

21
src/Processors/ToDo.php Normal file
View File

@ -0,0 +1,21 @@
<?php
namespace Aerex\TaskwarriorPlugin;
/**
* Class iCalEvent
*
* @author yourname
*/
class ToDo implements CalendarComponentInterface
{
public
public function import(){
}
}
?>

29
src/Task.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use DavidBadura\Taskwarrior\Task as BasicTask;
/**
* Task
* @author Aerex
*/
class Task extends BasicTask {
/**
*
* {@inheritDoc}
*
* If description is not available attempt to summary, otherwise throw Exception
*/
public function setDescription(VTodo $toDoComponent){
if(!isset($ToDoComponent->DESCRIPTION) && isset($ToDoComponent->SUMMARY)){
$description = $summary;
} else if(!isset($ToDoComponent->DESCRIPTION) && !isset($ToDoComponent->SUMMARY)){
throw new Exception("Task must have a description or summary");
}
}
public
}

View File

@ -61,6 +61,7 @@ class iCalEventProcessor {
public function importTask(VTodo $ToDoComponent = null){ public function importTask(VTodo $ToDoComponent = null){
echo $ToDoComponent->serialize();
if(!isset($ToDoComponent)){ if(!isset($ToDoComponent)){
$this->logger->error("vCal ToDo component is not defined"); $this->logger->error("vCal ToDo component is not defined");
throw new Exception("vCal Todo component is not defined"); throw new Exception("vCal Todo component is not defined");
@ -68,43 +69,71 @@ class iCalEventProcessor {
if($this->taskwarrior->exists($ToDoComponent->UID)){ if($this->taskwarrior->exists($ToDoComponent->UID)){
$this->logger->error("Event already exists " . (string)$ToDoComponent->UID); $this->taskwarrior->updateTask($ToDoComponent);
$this->logger->error("Updating task " . (string)$ToDoComponent->UID);
} }
try { try {
$task = $this->taskwarrior->createiCalTask($ToDoComponent->UID);
// get description
if(!isset($ToDoComponent->DESCRIPTION) && isset($ToDoComponent->SUMMARY)){
$description = $summary;
} else if(!isset($ToDoComponent->DESCRIPTION) && !isset($ToDoComponent->SUMMARY)){
throw new Exception("Task must have a description or summary");
}
// 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);
$start = $ToDoComponent->DSTART;
$entry = $ToDoComponent->DSTAMP;
$modified = $ToDoComponent->{'LAST-MODIFIED'};
$end = $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); $task->setDescription($ToDoComponent->DESCRIPTION);
// override protected taskwarrior properties using iCal event // get start time
if(!isset($ToDoComponent->DTSTAMP) && isset($ToDoComponent->DSTART)){
$start = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTSTAMP);
$entry = $start;
} else if (!isset($ToDoComponent->DTSTAMP) && !isset($ToDoComponent->DSTART)){
throw new Exception("Task must have an entry or start time");
} else {
$entry = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTSTAMP);
}
$this->taskwarrior->setValue($task, $this->taskwarrior::ENTRY, $entry); $this->taskwarrior->setValue($task, $this->taskwarrior::ENTRY, $entry);
$this->taskwarrior->setValue($task, $this->taskwarrior::START, $start);
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified); if(isset($ToDoComponent->DSTART)){
$this->taskwarrior->setValue($task, $this->taskwarrior::END, $end); $start = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DSTART);
$this->taskwarrior->setValue($task, $this->taskwarrior::START, $start);
}
if(isset($ToDoComponent->{'LAST-MODIFIED'})){
$modified = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->{'LAST-MODIFIED'});
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
}
if(isset($ToDoComponent->DTEND)){
$end = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTEND);
$this->taskwarrior->setValue($task, $this->taskwarrior::MODIFIED, $modified);
}
if(isset($ToDoComponent->DUE)){
$due = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DUE);
$task->setDue($due);
}
if(isset($ToDoComponent->DTEND)){
$end = $this->taskwarrior->convertToCarbonDateTime($ToDoComponent->DTEND);
$this->taskwarrior->setValue($task, $this->taskwarrior::END, $end);
}
if(isset($ToDoComponent->CATEGORIES)){
$tags = $this->taskwarrior->convertToStringArray($ToDoComponent->CATEGORIES);
$task->setTags($tags);
}
$this->taskwarrior->save($task); $this->taskwarrior->save($task);
} catch(Exception $e){ } catch(Exception $e){
echo $e->getMessage();
$this->logger->error($e->message); $this->logger->error($e->message);
throw $e; throw $e;