implementing workflow on tasks

This commit is contained in:
2018-04-25 15:35:52 +02:00
parent 4fe9c4296e
commit c99583b665
13 changed files with 429 additions and 12 deletions

View File

@@ -0,0 +1,92 @@
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\TaskBundle\Workflow\Definition;
use Chill\TaskBundle\Entity\AbstractTask;
use Chill\TaskBundle\Entity\SingleTask;
use Symfony\Component\Workflow\Transition;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class DefaultTaskDefinition implements \Chill\TaskBundle\Workflow\TaskWorkflowDefinition
{
const TRANSITION_METADATA = [
'close' => [
'verb' => 'close',
'background-color' => 'var(--yellow)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
],
'cancel' => [
'verb' => 'cancel',
'background-color' => 'var(--red)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
],
'start' => [
'verb' => 'start',
'background-color' => 'var(--green)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
]
];
public function supports(AbstractTask $task)
{
return $task instanceof SingleTask
&& $task->getType() === 'task_default';
}
public function getAssociatedWorkflowName()
{
return 'task_default';
}
public function getWorkflowMetadata(
string $key,
$metadataSubject = null
) {
$keys = \explode('.', $key);
switch($keys[0]) {
case 'transition':
if (!$metadataSubject instanceof Transition) {
throw new \LogicException("You must give a transition as metadatasubject");
}
return $this->getTransitionMetadata(\implode('.', \array_slice($keys, 1)), $metadataSubject);
default:
throw new \LogicException("this key '$key' is not implemented");
}
}
protected function getTransitionMetadata($key, Transition $transition)
{
if (!\array_key_exists($transition->getName(), self::TRANSITION_METADATA)) {
throw new \LogicException("the metadata for this transition are not defined");
}
if (!\array_key_exists($key, self::TRANSITION_METADATA[$transition->getName()])) {
throw new \LogicException("The metadata ".$key." is not "
. "defined for the transition ".$transition.getName());
}
return self::TRANSITION_METADATA[$transition->getName()][$key];
}
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\TaskBundle\Workflow;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
interface TaskWorkflowDefinition
{
}

View File

@@ -28,14 +28,56 @@ use Symfony\Component\Workflow\Workflow;
*/
class TaskWorkflowManager implements SupportStrategyInterface
{
/**
*
* @var TaskWorkflowDefinition[]
*/
protected $definitions = array();
public function addDefinition(TaskWorkflowDefinition $definition) {
$this->definitions[] = $definition;
}
/**
*
* @param AbstractTask $task
* @return TaskWorkflowDefinition
* @throws \LogicException
*/
public function getTaskWorkflowDefinition(AbstractTask $task)
{
$definitions = array();
foreach($this->definitions as $tested) {
if ($tested->supports($task)) {
$definitions[] = $tested;
}
}
$count = count($definitions);
if ($count > 1) {
throw new \LogicException("More than one TaskWorkflowDefinition supports "
. "this task. This should not happens.");
} elseif ($count === 0) {
throw new \LogicException("No taskWorkflowDefinition supports this task.");
}
return $definitions[0];
}
public function supports(Workflow $workflow, $subject): bool
{
if (!$subject instanceof AbstractTask) {
return false;
}
dump($workflow->getName());
return $workflow->getName() === 'task_default';
return $workflow->getName() === $this
->getTaskWorkflowDefinition($subject)->getAssociatedWorkflowName();
}
public function getWorkflowMetadata(AbstractTask $task, string $key, $metadataSubject = null, string $name = null)
{
return $this->getTaskWorkflowDefinition($task)
->getWorkflowMetadata($key, $metadataSubject);
}
}