TaskwarriorPlugin/src/TW/Commands/TodoStrategy.php
Aerex 06e67dec3c fix: Used correct namespace
- fix: Removed <? at end of file
2020-02-26 01:37:23 -06:00

116 lines
2.5 KiB
PHP

<?php
namespace Aerex\TaskwarriorPlugin\TW\Commands;
use Aerex\TaskwarriorPlugin\TW\Task;
use Aerex\TaskwarriorPlugin\Config;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Aerex\TaskwarriorPlugin\Exceptions\TaskwarriorCommandException;
class TodoStrategy implements IStrategy {
private $config;
public function __construct(Config $config){
$this->config = $config;
}
public function count($uuid){
$cmd[] = $this->config->getTaskBin();
$cmd[] = sprintf('%s count', $uuid);
return $this->executeCommand($cmd);
}
public function modify($task){
$cmd[] = $this->config->getTaskBin();
$uuid = $task->getUuid();
$taskJson = $task->convertToArray();
// Append modify command
$cmd[] = sprintf(' %s modify ', $uuid);
$document = $task->getDescription();
// Append as first modifier description if set
if(isset($document)){
$cmd[] = $document;
}
// Append on modifiers
foreach($taskJson as $prop => $value){
if(isset($value) && $value != null){
$cmd[] = sprintf(' %s: %s ', $prop, $value);
}
}
return $this->executeCommand($cmd);
}
public function add(Task $task){
$cmd[] = $this->config->getTaskBin();
$cmd[] = 'add';
if($task->getDescription() != null){
$cmd[] = sprintf('"%s"', (string)$task->getDescription());
}
if($task->getCategories() != null){
$categories = implode(' +', (string)$task->getCategories());
$cmd[] = $categories;
}
if($task->getDue() != null){
$cmd[] = sprintf("due:%s",$task->getDue('Y-m-dTH:i:s'));
}
if($task->getRecurrence() != null){
$rrule = $task->getRecurrence()->getParts();
$cmd[] = sprintf('recur:%s', $rrule['FREQ']);
if(isset($rrule['UNTIL'])){
$cmd[] = sprintf('until:%s', $rrule['UNTIL']);
}
}
if($task->getStatus() != null){
$cmd[] = sprintf('status:%s', (string)$task->getStatus());
}
return $this->executeCommand($cmd);
}
private function executeCommand($command){
$rcOptions = $this->config->getOptions();
foreach ($rcOptions as $rcOption) {
$command[] = $rcOption;
}
$cmdString = implode(' ', $command);
echo $cmdString;
$process = new Process($cmdString);
try {
$process->mustRun();
// clear cmd queue
$this->cmd = [];
return $process->getOutput();
} catch(ProcessFailedException $error){
throw new TaskwarriorCommandException($error->getMesage());
return;
}
}
}