Improve automatic actions (check for compatible events/actions/parameters)

This commit is contained in:
Frédéric Guillot
2014-09-28 14:26:40 -04:00
parent 0c8de6a3f5
commit 03fa01ac7b
33 changed files with 485 additions and 361 deletions

View File

@@ -3,12 +3,17 @@
namespace Action;
use Core\Listener;
use Core\Registry;
use Core\Tool;
/**
* Base class for automatic actions
*
* @package action
* @author Frederic Guillot
*
* @property \Model\Acl $acl
* @property \Model\Task $task
*/
abstract class Base implements Listener
{
@@ -28,6 +33,22 @@ abstract class Base implements Listener
*/
private $params = array();
/**
* Attached event name
*
* @access protected
* @var string
*/
protected $event_name = '';
/**
* Registry instance
*
* @access protected
* @var \Core\Registry
*/
protected $registry;
/**
* Execute the action
*
@@ -56,15 +77,60 @@ abstract class Base implements Listener
*/
abstract public function getEventRequiredParameters();
/**
* Get the compatible events
*
* @abstract
* @access public
* @return array
*/
abstract public function getCompatibleEvents();
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
abstract public function hasRequiredCondition(array $data);
/**
* Constructor
*
* @access public
* @param integer $project_id Project id
* @param \Core\Registry $registry Regsitry instance
* @param integer $project_id Project id
* @param string $event_name Attached event name
*/
public function __construct($project_id)
public function __construct(Registry $registry, $project_id, $event_name)
{
$this->registry = $registry;
$this->project_id = $project_id;
$this->event_name = $event_name;
}
/**
* Return class information
*
* @access public
* @return string
*/
public function __toString()
{
return get_called_class();
}
/**
* Load automatically models
*
* @access public
* @param string $name Model name
* @return mixed
*/
public function __get($name)
{
return Tool::loadModel($this->registry, $name);
}
/**
@@ -101,11 +167,34 @@ abstract class Base implements Listener
*/
public function isExecutable(array $data)
{
if (isset($data['project_id']) && $data['project_id'] == $this->project_id && $this->hasRequiredParameters($data)) {
return true;
}
return $this->hasCompatibleEvent() &&
$this->hasRequiredProject($data) &&
$this->hasRequiredParameters($data) &&
$this->hasRequiredCondition($data);
}
return false;
/**
* Check if the event is compatible with the action
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasCompatibleEvent()
{
return in_array($this->event_name, $this->getCompatibleEvents());
}
/**
* Check if the event data has the required project
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredProject(array $data)
{
return isset($data['project_id']) && $data['project_id'] == $this->project_id;
}
/**
@@ -118,7 +207,9 @@ abstract class Base implements Listener
public function hasRequiredParameters(array $data)
{
foreach ($this->getEventRequiredParameters() as $parameter) {
if (! isset($data[$parameter])) return false;
if (! isset($data[$parameter])) {
return false;
}
}
return true;
@@ -139,15 +230,4 @@ abstract class Base implements Listener
return false;
}
/**
* Return class information
*
* @access public
* @return string
*/
public function __toString()
{
return get_called_class();
}
}

View File

@@ -13,24 +13,16 @@ use Model\Task;
class TaskAssignCategoryColor extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_CREATE_UPDATE,
);
}
/**
@@ -62,7 +54,7 @@ class TaskAssignCategoryColor extends Base
}
/**
* Execute the action
* Execute the action (change the category)
*
* @access public
* @param array $data Event data dictionary
@@ -70,16 +62,23 @@ class TaskAssignCategoryColor extends Base
*/
public function doAction(array $data)
{
if ($data['color_id'] == $this->getParam('color_id')) {
$values = array(
'id' => $data['task_id'],
'category_id' => $this->getParam('category_id'),
);
$this->task->update(array(
'id' => $data['task_id'],
'category_id' => $this->getParam('category_id'),
), false);
return $this->task->update($values, false);
}
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['color_id'] == $this->getParam('color_id');
}
}

View File

@@ -13,24 +13,16 @@ use Model\Task;
class TaskAssignColorCategory extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_CREATE_UPDATE,
);
}
/**
@@ -62,7 +54,7 @@ class TaskAssignColorCategory extends Base
}
/**
* Execute the action
* Execute the action (change the task color)
*
* @access public
* @param array $data Event data dictionary
@@ -70,16 +62,23 @@ class TaskAssignColorCategory extends Base
*/
public function doAction(array $data)
{
if ($data['category_id'] == $this->getParam('category_id')) {
$values = array(
'id' => $data['task_id'],
'color_id' => $this->getParam('color_id'),
);
$this->task->update(array(
'id' => $data['task_id'],
'color_id' => $this->getParam('color_id'),
), false);
return $this->task->update($values, false);
}
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['category_id'] == $this->getParam('category_id');
}
}

View File

@@ -13,24 +13,17 @@ use Model\Task;
class TaskAssignColorUser extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_CREATE,
Task::EVENT_ASSIGNEE_CHANGE,
);
}
/**
@@ -62,7 +55,7 @@ class TaskAssignColorUser extends Base
}
/**
* Execute the action
* Execute the action (change the task color)
*
* @access public
* @param array $data Event data dictionary
@@ -70,16 +63,23 @@ class TaskAssignColorUser extends Base
*/
public function doAction(array $data)
{
if ($data['owner_id'] == $this->getParam('user_id')) {
$values = array(
'id' => $data['task_id'],
'color_id' => $this->getParam('color_id'),
);
$this->task->update(array(
'id' => $data['task_id'],
'color_id' => $this->getParam('color_id'),
), false);
return $this->task->update($values, false);
}
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['owner_id'] == $this->getParam('user_id');
}
}

View File

@@ -3,7 +3,6 @@
namespace Action;
use Model\Task;
use Model\Acl;
/**
* Assign a task to the logged user
@@ -14,34 +13,17 @@ use Model\Acl;
class TaskAssignCurrentUser extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Acl model
*
* @accesss private
* @var \Model\Acl
*/
private $acl;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @param \Model\Acl $acl Acl model instance
* @return array
*/
public function __construct($project_id, Task $task, Acl $acl)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
$this->acl = $acl;
return array(
Task::EVENT_CREATE,
Task::EVENT_MOVE_COLUMN,
);
}
/**
@@ -80,16 +62,23 @@ class TaskAssignCurrentUser extends Base
*/
public function doAction(array $data)
{
if ($data['column_id'] == $this->getParam('column_id')) {
$values = array(
'id' => $data['task_id'],
'owner_id' => $this->acl->getUserId(),
);
$this->task->update(array(
'id' => $data['task_id'],
'owner_id' => $this->acl->getUserId(),
), false);
return $this->task->update($values, false);
}
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id');
}
}

View File

@@ -13,24 +13,17 @@ use Model\Task;
class TaskAssignSpecificUser extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_CREATE_UPDATE,
Task::EVENT_MOVE_COLUMN,
);
}
/**
@@ -62,7 +55,7 @@ class TaskAssignSpecificUser extends Base
}
/**
* Execute the action
* Execute the action (assign the given user)
*
* @access public
* @param array $data Event data dictionary
@@ -70,16 +63,23 @@ class TaskAssignSpecificUser extends Base
*/
public function doAction(array $data)
{
if ($data['column_id'] == $this->getParam('column_id')) {
$values = array(
'id' => $data['task_id'],
'owner_id' => $this->getParam('user_id'),
);
$this->task->update(array(
'id' => $data['task_id'],
'owner_id' => $this->getParam('user_id'),
), false);
return $this->task->update($values, false);
}
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id');
}
}

View File

@@ -2,6 +2,7 @@
namespace Action;
use Model\GithubWebhook;
use Model\Task;
/**
@@ -13,24 +14,17 @@ use Model\Task;
class TaskClose extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_MOVE_COLUMN,
GithubWebhook::EVENT_COMMIT,
);
}
/**
@@ -41,9 +35,12 @@ class TaskClose extends Base
*/
public function getActionRequiredParameters()
{
return array(
'column_id' => t('Column'),
);
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
return array();
default:
return array('column_id' => t('Column'));
}
}
/**
@@ -54,14 +51,16 @@ class TaskClose extends Base
*/
public function getEventRequiredParameters()
{
return array(
'task_id',
'column_id',
);
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
return array('task_id');
default:
return array('task_id', 'column_id');
}
}
/**
* Execute the action
* Execute the action (close the task)
*
* @access public
* @param array $data Event data dictionary
@@ -69,11 +68,23 @@ class TaskClose extends Base
*/
public function doAction(array $data)
{
if ($data['column_id'] == $this->getParam('column_id')) {
$this->task->close($data['task_id']);
return true;
}
return $this->task->close($data['task_id']);
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
switch ($this->event_name) {
case GithubWebhook::EVENT_COMMIT:
return true;
default:
return $data['column_id'] == $this->getParam('column_id');
}
}
}

View File

@@ -13,24 +13,17 @@ use Model\Task;
class TaskDuplicateAnotherProject extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_MOVE_COLUMN,
Task::EVENT_CLOSE,
);
}
/**
@@ -63,7 +56,7 @@ class TaskDuplicateAnotherProject extends Base
}
/**
* Execute the action
* Execute the action (duplicate the task to another project)
*
* @access public
* @param array $data Event data dictionary
@@ -71,14 +64,20 @@ class TaskDuplicateAnotherProject extends Base
*/
public function doAction(array $data)
{
if ($data['column_id'] == $this->getParam('column_id') && $data['project_id'] != $this->getParam('project_id')) {
$task = $this->task->getById($data['task_id']);
$this->task->duplicateToAnotherProject($this->getParam('project_id'), $task);
return true;
}
$task = $this->task->getById($data['task_id']);
$this->task->duplicateToAnotherProject($this->getParam('project_id'), $task);
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id') && $data['project_id'] != $this->getParam('project_id');
}
}

View File

@@ -13,24 +13,17 @@ use Model\Task;
class TaskMoveAnotherProject extends Base
{
/**
* Task model
*
* @accesss private
* @var \Model\Task
*/
private $task;
/**
* Constructor
* Get the list of compatible events
*
* @access public
* @param integer $project_id Project id
* @param \Model\Task $task Task model instance
* @return array
*/
public function __construct($project_id, Task $task)
public function getCompatibleEvents()
{
parent::__construct($project_id);
$this->task = $task;
return array(
Task::EVENT_MOVE_COLUMN,
Task::EVENT_CLOSE,
);
}
/**
@@ -63,7 +56,7 @@ class TaskMoveAnotherProject extends Base
}
/**
* Execute the action
* Execute the action (move the task to another project)
*
* @access public
* @param array $data Event data dictionary
@@ -71,14 +64,20 @@ class TaskMoveAnotherProject extends Base
*/
public function doAction(array $data)
{
if ($data['column_id'] == $this->getParam('column_id') && $data['project_id'] != $this->getParam('project_id')) {
$task = $this->task->getById($data['task_id']);
$this->task->moveToAnotherProject($this->getParam('project_id'), $task);
return true;
}
$task = $this->task->getById($data['task_id']);
$this->task->moveToAnotherProject($this->getParam('project_id'), $task);
return true;
}
return false;
/**
* Check if the event data meet the action condition
*
* @access public
* @param array $data Event data dictionary
* @return bool
*/
public function hasRequiredCondition(array $data)
{
return $data['column_id'] == $this->getParam('column_id') && $data['project_id'] != $this->getParam('project_id');
}
}