refactor: clean up code and remove some dependencies

This commit is contained in:
2018-11-25 17:35:33 -06:00
parent 2d2122e05d
commit 98b67855ca
19 changed files with 1075 additions and 565 deletions

43
src/CalendarProcessor.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace Aerex\TaskwarriorPlugin\Processors;
use Aerex\TaskwarriorPlugin\TaskwarriorManager;
use Sabre\VObject\Component\VTodo;
/**
* Class ToDo
*
* @author Aerex
*/
class CalendarProcessor
{
public function __construct(TaskwarriorManager $taskwarriorManager){
$this->taskwarriorManager = $taskwarriorManager;
}
public function importTask(VTodo $Todo){
if($this->taskwarriorManager->exists($Todo->UID)){
$this->taskwarriorManager->updateTask($Todo);
}
try {
$this->taskwarriorManager->addTask($Todo);
} catch(Exception $e){
echo $e->getMessage();
throw $e;
}
}
public function export(){
echo "Not yet implemented";
}
}
?>

View File

@@ -1,6 +1,6 @@
<?php
namespace Aerex\TaskwarriorPlugin;
namespace Aerex\TaskwarriorPlugin\Configuration;
use DavidBadura\Taskwarrior\Taskwarrior;
use Aerex\TaskwarriorPlugin\TaskwarriorManager;

View File

@@ -3,13 +3,13 @@
namespace Aerex\TaskwarriorPlugin;
use Sabre\DAV\Exception\BadRequest;
use Sabre\VObject\Document;
use Sabre\VObject\Component\VCalendar;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\Xml\ParseException;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Server;
use Aerex\TaskwarriorPlugin\iCalEventProcessor;
use Aerex\TaskwarriorPlugin\CalendarProcessor;
use Aerex\TaskwarriorPlugin\Config;
/**
@@ -49,7 +49,7 @@ class Plugin extends ServerPlugin {
if(!is_null($TWCalManager)){
$this->twCalManager = $TWCalManager;
} else {
$this->twCalManager = new iCalEventProcessor();
$this->twCalManager = new CalendarProcessor();
}
}
@@ -100,7 +100,7 @@ class Plugin extends ServerPlugin {
*
* @param VCalendar $vCal parsed calendar object
*/
function processCalendarEventForTaskwarrior(Document $vCal){
function processCalendarEventForTaskwarrior(VCalendar $vCal){
try {
$this->twCalManager->importTask($vCal->VTODO);
} catch(BadRequest $e){

View File

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

View File

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

View File

@@ -1,29 +0,0 @@
<?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

@@ -0,0 +1,10 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands;
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
interface Strategy {
public function add(Task $task);
}
?>

View File

@@ -0,0 +1,62 @@
<?php
class TodoStrategy implements IStrategy {
public function __construct(TaskwarriorConfig $config){
$this->config = $config;
$this->cmd[] = $this->config->taskBin();
}
public function add(Task $task){
$this->cmd[] = 'add';
if($task->getDescription() != null){
$this->cmd[] = sprintf('"%s"', $task->getDescription());
}
if($task->getCategories() != null){
$categories = implode(' +', $task->getCategories());
$this->cmd[] = $categories;
}
if($task->getDue() != null){
$this->cmd[] = $task->getDue('Y-m-dTH:i:s');
}
if($task->getRecurrence() != null){
$rrule = $task->getRecurrence()->getParts();
$this->cmd[] = sprintf('recur:%s', $rrule['FREQ']);
if(isset($rrule['UNTIL'])){
$this->cmd[] = sprintf('until:%s', $rrule['UNTIL']);
}
}
if($task->getStatus() != null){
$this->cmd[] = sprintf('status:%s', $task->getStatus());
}
return $this->executeCommand($cmd);
}
private function executeCommand($command){
$cmdString = implode(' ', $command);
$process = new Process($cmdString);
$process->run();
if(!$process->isSuccessful()){
throw new TaskwarriorCommandLineException($process);
}
return $process->getOutput();
}
}

264
src/Taskwarrior/Task.php Normal file
View File

@@ -0,0 +1,264 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
use Zend\Validator\Uuid;
use Carbon\Carbon;
use Sabre\VObject\Component\VTodo;
/**
* Task
* @author Aerex
*/
class Task {
/**
* @var string
*
* @JMS\Type("string")
*/
private $uuid;
/**
* @var string
*
* @JMS\Type("string")
*/
private $description;
/**
* @var string
*
* @JMS\Type("string")
*/
private $priority;
/**
* @var string
*
* @JMS\Type("string")
*/
private $project;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $due;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $wait;
/**
* @var array
*
* @JMS\Type("array<string>")
*/
private $tags;
/**
* @var float
*
* @JMS\Type("float")
*/
private $urgency;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $entry;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $start;
/**
* @var string
*
* @JMS\Type("Recurring")
*/
private $recur;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $until;
/**
* @var Annotation[]
*
* @JMS\Type("array<Aerex\Taskwarrior\Annotation>")
*/
private $annotations = [];
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $modified;
/**
* @var Carbon
*
* @JMS\Type("Carbon")
*/
private $end;
/**
* @var string
*
* @JMS\Type("string")
*/
private $status;
/**
* @var Task[]|ArrayCollection
*
* @JMS\Type("Depends")
*/
private $depends;
public function __construct($UUID=null){
$validator = new Uuid();
if(!isset($UUID)){
$this->uuid = uniqid();
} else if(isset($UUID) && !$validator->isValid($UUID)){
throw new Exception(sprintf('%s is not a valid uuid', $UUID));
}
$this->uuid = $UUID;
}
/**
*
* {@inheritDoc}
*
* If description is not available attempt to summary, otherwise throw Exception
*/
public function setDescription(VTodo $component){
if(!isset($component->DESCRIPTION) && isset($component->SUMMARY)){
$this->description = $component->SUMMARY;
} else if(!isset($component->DESCRIPTION) && !isset($component->SUMMARY)){
throw new Exception("Task must have a description or summary");
} else {
$this->description = $component->DESCRIPTION;
}
}
public function getDescription(){
return $this->description;
}
public function setEntryTime(VTodo $document){
if(isset($document->DTSTAMP)){
$this->entry = new Carbon($document->DTSTAMP->getDateTime()->format(\DateTime::W3C));
} else {
throw new Exception('Task must have a entry time');
}
}
public function getEntryTime(){
return $this->entry;
}
public function setStartTime(VTodo $document){
if(isset($document->DTSTART)){
$this->start = new Carbon($document->DTSTART->getDateTime()->format(\DateTime::W3C));
}
}
public function getStartTime(){
return $this->start;
}
public function setModifiedTime(VTodo $document){
if(isset($document->{'LAST-MODIFIED'})){
$this->modified = new Carbon($document->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C));
}
}
public function getModifiedTime(){
return $this->modified;
}
public function setDue(VTodo $document){
if(isset($document->DUE)){
$this->due = new Carbon($document->DUE->getDateTime()->format(\DateTime::W3C));
}
}
public function getDue(){
return $this->due;
}
public function setStopTime(VTodo $document){
if(isset($document->DTEND)){
$this->end = new Carbon($document->DTEND->getDateTime()->format(\DateTime::W3C));
}
}
public function getStopTime(){
return $this->end;
}
public function setCategories(VTodo $document){
if(isset($document->CATEGORIES)){
$this->tags = explode(',', (string)$document->CATEGORIES);
}
}
public function getCategories(){
return $this->tags;
}
public function setStatus(VTodo $document){
if(isset($document->STATUS)){
switch((string)$document->STATUS){
case 'NEEDS-ACTION':
$this->status = 'pending';
break;
case 'COMPLETED':
$this->status = 'completed';
break;
case 'CANCELED':
$this->status = 'deleted';
break;
}
}
}
public function setRecurrence(VTodo $document){
if(isset($document->RRULE)){
$this->recur = $document->RRULE;
}
}
public function getStatus(){
return $this->status;
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace Aerex\TaskwarriorPlugin\Taskwarrior;
use Symfony\Component\Process\Process;
use Aerex\TaskwarriorPlugin\Commands\IStrategy;
class Taskwarrior {
const EXPORT = 'export';
const IMPORT = 'import';
const ADD = 'add';
/**
* @var TaskwarriorConfig
*/
private $config;
/**
* @var string
*/
private $bin;
/**
* @var string
*/
private $taskrc;
/**
* @var string
*/
private $taskData;
/**
* @var array
*/
private $rcOptions;
/**
* @var string
*/
public function __construct(TaskwarriorConfig $config){
if(!isset($config)){
$this->config = new TaskwarriorConfig();
}
$this->config = $config;
}
public function setStrategy(IStrategy $strategy){
$this->strategy = $strategy;
}
public function createTask($uuid){
$task = new Task($uuid);
return $task;
}
public function add($task){
$this->strategy($task);
}
}
?>

View File

@@ -0,0 +1,158 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Taskwarrior;
use Sabre\VObject\Component\VTodo;
use DavidBadura\Taskwarrior\TaskManager;
use DavidBadura\Taskwarrior\Task;
class TaskwarriorManager {
const DESCRIPTION = 'description';
const CATEGORIES = 'categories';
const TASK_UUID = 'uuid';
const ICAL_UID = 'uid';
private $tasks;
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();
}
public function export(VTodo $document){
}
public function addTask(VTodo $document){
$task = $this->taskwarrior->createTask($document->UID);
$task->setDescription($document);
$task->setEntry($document);
$task->setStartTime($document);
$task->setModifiedTime($document);
$task->setStopTime($document);
$task->setDue($document);
$task->setCategories($document);
$task->setStatus($document);
$task->setRecurrence($document);
return $this->taskwarrior->add($task);
}
public function updateTask(VTodo $document){
$task = $this->taskwarrior->get($document->UID);
$task->setDescription($document);
$task->setEntry($document);
$task->setStartTime($document);
$task->setModifiedTime($document);
$task->setStopTime($document);
$task->setDue($document);
$task->setCategories($document);
$task->setStatus($document);
$task->setRecurrence($document);
$updatedTask = $this->taskwarrior->modify($task);
return $upatedTask;
}
function setEntryTime($entry){
}
function exists($UUID){}
function setEndTime($end){}
function setModifiedTime($modifiedTime){}
function setSummary($description){
if(!isset($description)){
return;
}
$this->taskWarriorJSON[self::DESCRIPTION] = $description;
}
function setStartTime($startTime){}
function setCategories($categories){
if(!isset($categories)){
return;
}
if(!is_array($catergories)){
return;
}
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
}
public function taskExists($taskUuid){
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
if($taskIsInCache){
return true;
}
$jsonArray = $this->taskwarrior->export($taskUuid);
$taskWithUuidExists = count($jsonArray) > 0;
return $taskWithUuidExists;
}
public function save(){
}
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){}
}
?>

View File

@@ -1,112 +0,0 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Taskwarrior;
use DavidBadura\Taskwarrior\TaskManager;
use DavidBadura\Taskwarrior\Task;
class TaskwarriorManager extends TaskManager {
const DESCRIPTION = 'description';
const CATEGORIES = 'categories';
const TASK_UUID = 'uuid';
const ICAL_UID = 'uid';
/**
*
* @var \DavidBadura\Taskwarrior\Task
*/
private $tasks;
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){
}
function exists($UUID){}
function setEndTime($end){}
function setModifiedTime($modifiedTime){}
function setSummary($description){
if(!isset($description)){
return;
}
$this->taskWarriorJSON[self::DESCRIPTION] = $description;
}
function setStartTime($startTime){}
function setCategories($categories){
if(!isset($categories)){
return;
}
if(!is_array($catergories)){
return;
}
$this->taskWarriorJSON[self::CATEGORIES] = $categories;
}
public function taskExists($taskUuid){
$taskIsInCache = isset($this->cachedTasks[$taskUuid]);
if($taskIsInCache){
return true;
}
$jsonArray = $this->taskwarrior->export($taskUuid);
$taskWithUuidExists = count($jsonArray) > 0;
return $taskWithUuidExists;
}
public function save(){
}
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){}
}
?>

View File

@@ -1,9 +0,0 @@
<?php
class TaskwarriorProps {
static $DESCRIPTION = 'description';
}
?>

View File

@@ -1,159 +0,0 @@
<?php
namespace Aerex\TaskwarriorPlugin;
use Aerex\TaskwarriorPlugin\Config;
use Sabre\DAV\Exception;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Component\VTodo;
class iCalEventProcessor {
/**
* @var Config
*/
private $config;
/**
* @var string
*
*/
private $taskrc;
/**
* @var string
*/
private $taskDataDir;
/**
* @var array
*/
private $cachedTasks = [];
/**
* @var string
*/
private $taskBinFile;
/**
* @var Taskwarrior
*/
private $taskwarrior;
public function __construct(Config $taskConfig = null){
if(!is_null($taskConfig)){
$this->taskConfig = $taskConfig;
} else {
$this->taskConfig = new Config();
}
$this->taskwarrior = $this->taskConfig->getTaskwarriorInstance();
$this->logger = $this->taskConfig->getLogger();
}
public function importTask(VTodo $ToDoComponent = null){
echo $ToDoComponent->serialize();
if(!isset($ToDoComponent)){
$this->logger->error("vCal ToDo component is not defined");
throw new Exception("vCal Todo component is not defined");
}
if($this->taskwarrior->exists($ToDoComponent->UID)){
$this->taskwarrior->updateTask($ToDoComponent);
$this->logger->error("Updating task " . (string)$ToDoComponent->UID);
}
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");
}
$task->setDescription($ToDoComponent->DESCRIPTION);
// 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);
if(isset($ToDoComponent->DSTART)){
$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);
} catch(Exception $e){
echo $e->getMessage();
$this->logger->error($e->message);
throw $e;
}
}
}
?>