summaryrefslogtreecommitdiff
path: root/app/Action/Base.php
diff options
context:
space:
mode:
authorGerardo Zamudio <gerardozamudio@users.noreply.github.com>2016-02-24 23:48:50 -0600
committerGerardo Zamudio <gerardozamudio@users.noreply.github.com>2016-02-24 23:48:50 -0600
commite4de6b3898b64b26d29aff31f21df5fda8055686 (patch)
tree575f8a65440f291d70a070d168eafca8c82a6459 /app/Action/Base.php
parentd9ffbea174ea6524d0a22f8375ca8b3aa04a3c96 (diff)
parenta6540bc604c837d92c9368540c145606723e97f7 (diff)
Merge pull request #1 from fguillot/master
Update from upstream
Diffstat (limited to 'app/Action/Base.php')
-rw-r--r--app/Action/Base.php132
1 files changed, 89 insertions, 43 deletions
diff --git a/app/Action/Base.php b/app/Action/Base.php
index 4d2d6da6..e8449d0c 100644
--- a/app/Action/Base.php
+++ b/app/Action/Base.php
@@ -3,7 +3,6 @@
namespace Kanboard\Action;
use Kanboard\Event\GenericEvent;
-use Pimple\Container;
/**
* Base class for automatic actions
@@ -14,6 +13,14 @@ use Pimple\Container;
abstract class Base extends \Kanboard\Core\Base
{
/**
+ * Extended events
+ *
+ * @access private
+ * @var array
+ */
+ private $compatibleEvents = array();
+
+ /**
* Flag for called listener
*
* @access private
@@ -27,7 +34,7 @@ abstract class Base extends \Kanboard\Core\Base
* @access private
* @var integer
*/
- private $project_id = 0;
+ private $projectId = 0;
/**
* User parameters
@@ -38,20 +45,25 @@ abstract class Base extends \Kanboard\Core\Base
private $params = array();
/**
- * Attached event name
+ * Get automatic action name
*
- * @access protected
- * @var string
+ * @final
+ * @access public
+ * @return string
*/
- protected $event_name = '';
+ final public function getName()
+ {
+ return '\\'.get_called_class();
+ }
/**
- * Container instance
+ * Get automatic action description
*
- * @access protected
- * @var \Pimple\Container
+ * @abstract
+ * @access public
+ * @return string
*/
- protected $container;
+ abstract public function getDescription();
/**
* Execute the action
@@ -100,30 +112,32 @@ abstract class Base extends \Kanboard\Core\Base
abstract public function hasRequiredCondition(array $data);
/**
- * Constructor
+ * Return class information
*
* @access public
- * @param \Pimple\Container $container Container
- * @param integer $project_id Project id
- * @param string $event_name Attached event name
+ * @return string
*/
- public function __construct(Container $container, $project_id, $event_name)
+ public function __toString()
{
- $this->container = $container;
- $this->project_id = $project_id;
- $this->event_name = $event_name;
- $this->called = false;
+ $params = array();
+
+ foreach ($this->params as $key => $value) {
+ $params[] = $key.'='.var_export($value, true);
+ }
+
+ return $this->getName().'('.implode('|', $params).')';
}
/**
- * Return class information
+ * Set project id
*
* @access public
- * @return string
+ * @return Base
*/
- public function __toString()
+ public function setProjectId($project_id)
{
- return get_called_class();
+ $this->projectId = $project_id;
+ return $this;
}
/**
@@ -134,7 +148,7 @@ abstract class Base extends \Kanboard\Core\Base
*/
public function getProjectId()
{
- return $this->project_id;
+ return $this->projectId;
}
/**
@@ -143,10 +157,12 @@ abstract class Base extends \Kanboard\Core\Base
* @access public
* @param string $name Parameter name
* @param mixed $value Value
+ * @param Base
*/
public function setParam($name, $value)
{
$this->params[$name] = $value;
+ return $this;
}
/**
@@ -154,24 +170,25 @@ abstract class Base extends \Kanboard\Core\Base
*
* @access public
* @param string $name Parameter name
- * @param mixed $default_value Default value
+ * @param mixed $default Default value
* @return mixed
*/
- public function getParam($name, $default_value = null)
+ public function getParam($name, $default = null)
{
- return isset($this->params[$name]) ? $this->params[$name] : $default_value;
+ return isset($this->params[$name]) ? $this->params[$name] : $default;
}
/**
* Check if an action is executable (right project and required parameters)
*
* @access public
- * @param array $data Event data dictionary
- * @return bool True if the action is executable
+ * @param array $data
+ * @param string $eventName
+ * @return bool
*/
- public function isExecutable(array $data)
+ public function isExecutable(array $data, $eventName)
{
- return $this->hasCompatibleEvent() &&
+ return $this->hasCompatibleEvent($eventName) &&
$this->hasRequiredProject($data) &&
$this->hasRequiredParameters($data) &&
$this->hasRequiredCondition($data);
@@ -181,11 +198,12 @@ abstract class Base extends \Kanboard\Core\Base
* Check if the event is compatible with the action
*
* @access public
+ * @param string $eventName
* @return bool
*/
- public function hasCompatibleEvent()
+ public function hasCompatibleEvent($eventName)
{
- return in_array($this->event_name, $this->getCompatibleEvents());
+ return in_array($eventName, $this->getEvents());
}
/**
@@ -197,7 +215,7 @@ abstract class Base extends \Kanboard\Core\Base
*/
public function hasRequiredProject(array $data)
{
- return isset($data['project_id']) && $data['project_id'] == $this->project_id;
+ return isset($data['project_id']) && $data['project_id'] == $this->getProjectId();
}
/**
@@ -222,10 +240,11 @@ abstract class Base extends \Kanboard\Core\Base
* Execute the action
*
* @access public
- * @param \Event\GenericEvent $event Event data dictionary
- * @return bool True if the action was executed or false when not executed
+ * @param \Kanboard\Event\GenericEvent $event
+ * @param string $eventName
+ * @return bool
*/
- public function execute(GenericEvent $event)
+ public function execute(GenericEvent $event, $eventName)
{
// Avoid infinite loop, a listener instance can be called only one time
if ($this->called) {
@@ -233,17 +252,44 @@ abstract class Base extends \Kanboard\Core\Base
}
$data = $event->getAll();
- $result = false;
+ $executable = $this->isExecutable($data, $eventName);
+ $executed = false;
- if ($this->isExecutable($data)) {
+ if ($executable) {
$this->called = true;
- $result = $this->doAction($data);
+ $executed = $this->doAction($data);
}
- if (DEBUG) {
- $this->container['logger']->debug(get_called_class().' => '.($result ? 'true' : 'false'));
+ $this->logger->debug($this.' ['.$eventName.'] => executable='.var_export($executable, true).' exec_success='.var_export($executed, true));
+
+ return $executed;
+ }
+
+ /**
+ * Register a new event for the automatic action
+ *
+ * @access public
+ * @param string $event
+ * @param string $description
+ */
+ public function addEvent($event, $description = '')
+ {
+ if ($description !== '') {
+ $this->eventManager->register($event, $description);
}
- return $result;
+ $this->compatibleEvents[] = $event;
+ return $this;
+ }
+
+ /**
+ * Get all compatible events of an automatic action
+ *
+ * @access public
+ * @return array
+ */
+ public function getEvents()
+ {
+ return array_unique(array_merge($this->getCompatibleEvents(), $this->compatibleEvents));
}
}