From 03fa01ac7b036820ee232d893ec63241918c6012 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 28 Sep 2014 14:26:40 -0400 Subject: Improve automatic actions (check for compatible events/actions/parameters) --- app/Action/Base.php | 116 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 18 deletions(-) (limited to 'app/Action/Base.php') diff --git a/app/Action/Base.php b/app/Action/Base.php index 5b7b3502..339aeecc 100644 --- a/app/Action/Base.php +++ b/app/Action/Base.php @@ -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(); - } } -- cgit v1.2.3