refactor: rename Taskwarrior folder TW
This commit is contained in:
115
src/TW/Commands/TodoStrategy.php
Normal file
115
src/TW/Commands/TodoStrategy.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands;
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior\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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user