diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-10-12 21:38:56 -0400 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-10-12 21:38:56 -0400 |
commit | 074056352de98fc567b4d13184c72887c75625d0 (patch) | |
tree | 7d262c3a5d5f779648f51aa0eb7d9f279c05d89d /app/Event | |
parent | 4061927d215c846ff8eb196301bf61532018042b (diff) |
Project activity refactoring and listeners improvements
Diffstat (limited to 'app/Event')
-rw-r--r-- | app/Event/Base.php | 79 | ||||
-rw-r--r-- | app/Event/BaseNotificationListener.php | 87 | ||||
-rw-r--r-- | app/Event/CommentHistoryListener.php | 73 | ||||
-rw-r--r-- | app/Event/CommentNotificationListener.php | 30 | ||||
-rw-r--r-- | app/Event/FileNotificationListener.php | 30 | ||||
-rw-r--r-- | app/Event/NotificationListener.php | 83 | ||||
-rw-r--r-- | app/Event/ProjectActivityListener.php | 61 | ||||
-rw-r--r-- | app/Event/ProjectModificationDate.php | 63 | ||||
-rw-r--r-- | app/Event/ProjectModificationDateListener.php | 30 | ||||
-rw-r--r-- | app/Event/SubTaskNotificationListener.php | 30 | ||||
-rw-r--r-- | app/Event/SubtaskHistoryListener.php | 73 | ||||
-rw-r--r-- | app/Event/TaskHistoryListener.php | 63 | ||||
-rw-r--r-- | app/Event/TaskNotificationListener.php | 29 | ||||
-rw-r--r-- | app/Event/WebhookListener.php | 32 |
14 files changed, 257 insertions, 506 deletions
diff --git a/app/Event/Base.php b/app/Event/Base.php new file mode 100644 index 00000000..745871a5 --- /dev/null +++ b/app/Event/Base.php @@ -0,0 +1,79 @@ +<?php + +namespace Event; + +use Core\Listener; +use Core\Registry; +use Core\Tool; + +/** + * Base Listener + * + * @package event + * @author Frederic Guillot + * + * @property \Model\Comment $comment + * @property \Model\Project $project + * @property \Model\ProjectActivity $projectActivity + * @property \Model\SubTask $subTask + * @property \Model\Task $task + * @property \Model\TaskFinder $taskFinder + */ +abstract class Base implements Listener +{ + /** + * Registry instance + * + * @access protected + * @var \Core\Registry + */ + protected $registry; + + /** + * Constructor + * + * @access public + * @param \Core\Registry $registry Regsitry instance + */ + public function __construct(Registry $registry) + { + $this->registry = $registry; + } + + /** + * 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); + } + + /** + * Get event namespace + * + * Event = task.close | Namespace = task + * + * @access public + * @return string + */ + public function getEventNamespace() + { + $event_name = $this->registry->event->getLastTriggeredEvent(); + return substr($event_name, 0, strpos($event_name, '.')); + } +} diff --git a/app/Event/BaseNotificationListener.php b/app/Event/BaseNotificationListener.php deleted file mode 100644 index fdabaf57..00000000 --- a/app/Event/BaseNotificationListener.php +++ /dev/null @@ -1,87 +0,0 @@ -<?php - -namespace Event; - -use Core\Listener; -use Model\Notification; - -/** - * Base notification listener - * - * @package event - * @author Frederic Guillot - */ -abstract class BaseNotificationListener implements Listener -{ - /** - * Notification model - * - * @accesss protected - * @var Model\Notification - */ - protected $notification; - - /** - * Template name - * - * @accesss private - * @var string - */ - private $template = ''; - - /** - * Fetch data for the mail template - * - * @access public - * @param array $data Event data - * @return array - */ - abstract public function getTemplateData(array $data); - - /** - * Constructor - * - * @access public - * @param \Model\Notification $notification Notification model instance - * @param string $template Template name - */ - public function __construct(Notification $notification, $template) - { - $this->template = $template; - $this->notification = $notification; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); - } - - /** - * Execute the action - * - * @access public - * @param array $data Event data dictionary - * @return bool True if the action was executed or false when not executed - */ - public function execute(array $data) - { - $values = $this->getTemplateData($data); - - // Get the list of users to be notified - $users = $this->notification->getUsersList($values['task']['project_id']); - - // Send notifications - if ($users) { - $this->notification->sendEmails($this->template, $users, $values); - return true; - } - - return false; - } -} diff --git a/app/Event/CommentHistoryListener.php b/app/Event/CommentHistoryListener.php deleted file mode 100644 index ec826615..00000000 --- a/app/Event/CommentHistoryListener.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php - -namespace Event; - -use Core\Listener; -use Model\CommentHistory; - -/** - * Comment history listener - * - * @package event - * @author Frederic Guillot - */ -class CommentHistoryListener implements Listener -{ - /** - * Comment History model - * - * @accesss private - * @var \Model\CommentHistory - */ - private $model; - - /** - * Constructor - * - * @access public - * @param \Model\CommentHistory $model Comment History model instance - */ - public function __construct(CommentHistory $model) - { - $this->model = $model; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); - } - - /** - * Execute the action - * - * @access public - * @param array $data Event data dictionary - * @return bool True if the action was executed or false when not executed - */ - public function execute(array $data) - { - $creator_id = $this->model->acl->getUserId(); - - if ($creator_id && isset($data['task_id']) && isset($data['id'])) { - - $task = $this->model->taskFinder->getById($data['task_id']); - - $this->model->create( - $task['project_id'], - $data['task_id'], - $data['id'], - $creator_id, - $this->model->event->getLastTriggeredEvent(), - $data['comment'] - ); - } - - return false; - } -} diff --git a/app/Event/CommentNotificationListener.php b/app/Event/CommentNotificationListener.php deleted file mode 100644 index 13803874..00000000 --- a/app/Event/CommentNotificationListener.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -namespace Event; - -use Event\BaseNotificationListener; - -/** - * Comment notification listener - * - * @package event - * @author Frederic Guillot - */ -class CommentNotificationListener extends BaseNotificationListener -{ - /** - * Fetch data for the mail template - * - * @access public - * @param array $data Event data - * @return array - */ - public function getTemplateData(array $data) - { - $values = array(); - $values['comment'] = $this->notification->comment->getById($data['id']); - $values['task'] = $this->notification->taskFinder->getDetails($values['comment']['task_id']); - - return $values; - } -} diff --git a/app/Event/FileNotificationListener.php b/app/Event/FileNotificationListener.php deleted file mode 100644 index 2366ce5d..00000000 --- a/app/Event/FileNotificationListener.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -namespace Event; - -use Event\BaseNotificationListener; - -/** - * File notification listener - * - * @package event - * @author Frederic Guillot - */ -class FileNotificationListener extends BaseNotificationListener -{ - /** - * Fetch data for the mail template - * - * @access public - * @param array $data Event data - * @return array - */ - public function getTemplateData(array $data) - { - $values = array(); - $values['file'] = $data; - $values['task'] = $this->notification->taskFinder->getDetails($data['task_id']); - - return $values; - } -} diff --git a/app/Event/NotificationListener.php b/app/Event/NotificationListener.php new file mode 100644 index 00000000..3c049327 --- /dev/null +++ b/app/Event/NotificationListener.php @@ -0,0 +1,83 @@ +<?php + +namespace Event; + +/** + * Notification listener + * + * @package event + * @author Frederic Guillot + */ +class NotificationListener extends Base +{ + /** + * Template name + * + * @accesss private + * @var string + */ + private $template = ''; + + /** + * Set template name + * + * @access public + * @param string $template Template name + */ + public function setTemplate($template) + { + $this->template = $template; + } + + /** + * Execute the action + * + * @access public + * @param array $data Event data dictionary + * @return bool True if the action was executed or false when not executed + */ + public function execute(array $data) + { + $values = $this->getTemplateData($data); + $users = $this->notification->getUsersList($values['task']['project_id']); + + if ($users) { + $this->notification->sendEmails($this->template, $users, $values); + return true; + } + + return false; + } + + /** + * Fetch data for the mail template + * + * @access public + * @param array $data Event data + * @return array + */ + public function getTemplateData(array $data) + { + $values = array(); + + switch ($this->getEventNamespace()) { + case 'task': + $values['task'] = $this->taskFinder->getDetails($data['task_id']); + break; + case 'subtask': + $values['subtask'] = $this->subtask->getById($data['id'], true); + $values['task'] = $this->taskFinder->getDetails($data['task_id']); + break; + case 'file': + $values['file'] = $data; + $values['task'] = $this->taskFinder->getDetails($data['task_id']); + break; + case 'comment': + $values['comment'] = $this->comment->getById($data['id']); + $values['task'] = $this->taskFinder->getDetails($values['comment']['task_id']); + break; + } + + return $values; + } +} diff --git a/app/Event/ProjectActivityListener.php b/app/Event/ProjectActivityListener.php new file mode 100644 index 00000000..8958bd2b --- /dev/null +++ b/app/Event/ProjectActivityListener.php @@ -0,0 +1,61 @@ +<?php + +namespace Event; + +/** + * Project activity listener + * + * @package event + * @author Frederic Guillot + */ +class ProjectActivityListener extends Base +{ + /** + * Execute the action + * + * @access public + * @param array $data Event data dictionary + * @return bool True if the action was executed or false when not executed + */ + public function execute(array $data) + { + if (isset($data['task_id'])) { + + $values = $this->getValues($data); + + return $this->projectActivity->createEvent( + $values['task']['project_id'], + $values['task']['id'], + $this->acl->getUserId(), + $this->registry->event->getLastTriggeredEvent(), + $values + ); + } + + return false; + } + + /** + * Get event activity data + * + * @access private + * @param array $data Event data dictionary + * @return array + */ + private function getValues(array $data) + { + $values = array(); + $values['task'] = $this->taskFinder->getDetails($data['task_id']); + + switch ($this->getEventNamespace()) { + case 'subtask': + $values['subtask'] = $this->subTask->getById($data['id'], true); + break; + case 'comment': + $values['comment'] = $this->comment->getById($data['id']); + break; + } + + return $values; + } +} diff --git a/app/Event/ProjectModificationDate.php b/app/Event/ProjectModificationDate.php deleted file mode 100644 index 1b0b3736..00000000 --- a/app/Event/ProjectModificationDate.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php - -namespace Event; - -use Core\Listener; -use Model\Project; - -/** - * Project modification date listener - * - * Update the last modified field for a project - * - * @package event - * @author Frederic Guillot - */ -class ProjectModificationDate implements Listener -{ - /** - * Project model - * - * @accesss private - * @var \Model\Project - */ - private $project; - - /** - * Constructor - * - * @access public - * @param \Model\Project $project Project model instance - */ - public function __construct(Project $project) - { - $this->project = $project; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); - } - - /** - * Execute the action - * - * @access public - * @param array $data Event data dictionary - * @return bool True if the action was executed or false when not executed - */ - public function execute(array $data) - { - if (isset($data['project_id'])) { - return $this->project->updateModificationDate($data['project_id']); - } - - return false; - } -} diff --git a/app/Event/ProjectModificationDateListener.php b/app/Event/ProjectModificationDateListener.php new file mode 100644 index 00000000..abc176b0 --- /dev/null +++ b/app/Event/ProjectModificationDateListener.php @@ -0,0 +1,30 @@ +<?php + +namespace Event; + +/** + * Project modification date listener + * + * Update the "last_modified" field for a project + * + * @package event + * @author Frederic Guillot + */ +class ProjectModificationDateListener extends Base +{ + /** + * Execute the action + * + * @access public + * @param array $data Event data dictionary + * @return bool True if the action was executed or false when not executed + */ + public function execute(array $data) + { + if (isset($data['project_id'])) { + return $this->project->updateModificationDate($data['project_id']); + } + + return false; + } +} diff --git a/app/Event/SubTaskNotificationListener.php b/app/Event/SubTaskNotificationListener.php deleted file mode 100644 index 2b35d757..00000000 --- a/app/Event/SubTaskNotificationListener.php +++ /dev/null @@ -1,30 +0,0 @@ -<?php - -namespace Event; - -use Event\BaseNotificationListener; - -/** - * SubTask notification listener - * - * @package event - * @author Frederic Guillot - */ -class SubTaskNotificationListener extends BaseNotificationListener -{ - /** - * Fetch data for the mail template - * - * @access public - * @param array $data Event data - * @return array - */ - public function getTemplateData(array $data) - { - $values = array(); - $values['subtask'] = $this->notification->subtask->getById($data['id'], true); - $values['task'] = $this->notification->taskFinder->getDetails($data['task_id']); - - return $values; - } -} diff --git a/app/Event/SubtaskHistoryListener.php b/app/Event/SubtaskHistoryListener.php deleted file mode 100644 index 6b4f1c2d..00000000 --- a/app/Event/SubtaskHistoryListener.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php - -namespace Event; - -use Core\Listener; -use Model\SubtaskHistory; - -/** - * Subtask history listener - * - * @package event - * @author Frederic Guillot - */ -class SubtaskHistoryListener implements Listener -{ - /** - * Comment History model - * - * @accesss private - * @var \Model\SubtaskHistory - */ - private $model; - - /** - * Constructor - * - * @access public - * @param \Model\SubtaskHistory $model Subtask History model instance - */ - public function __construct(SubtaskHistory $model) - { - $this->model = $model; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); - } - - /** - * Execute the action - * - * @access public - * @param array $data Event data dictionary - * @return bool True if the action was executed or false when not executed - */ - public function execute(array $data) - { - $creator_id = $this->model->acl->getUserId(); - - if ($creator_id && isset($data['task_id']) && isset($data['id'])) { - - $task = $this->model->taskFinder->getById($data['task_id']); - - $this->model->create( - $task['project_id'], - $data['task_id'], - $data['id'], - $creator_id, - $this->model->event->getLastTriggeredEvent(), - '' - ); - } - - return false; - } -} diff --git a/app/Event/TaskHistoryListener.php b/app/Event/TaskHistoryListener.php deleted file mode 100644 index d963fa71..00000000 --- a/app/Event/TaskHistoryListener.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php - -namespace Event; - -use Core\Listener; -use Model\TaskHistory; - -/** - * Task history listener - * - * @package event - * @author Frederic Guillot - */ -class TaskHistoryListener implements Listener -{ - /** - * Task History model - * - * @accesss private - * @var \Model\TaskHistory - */ - private $model; - - /** - * Constructor - * - * @access public - * @param \Model\TaskHistory $model Task History model instance - */ - public function __construct(TaskHistory $model) - { - $this->model = $model; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); - } - - /** - * Execute the action - * - * @access public - * @param array $data Event data dictionary - * @return bool True if the action was executed or false when not executed - */ - public function execute(array $data) - { - $creator_id = $this->model->acl->getUserId(); - - if ($creator_id && isset($data['task_id']) && isset($data['project_id'])) { - $this->model->create($data['project_id'], $data['task_id'], $creator_id, $this->model->event->getLastTriggeredEvent()); - } - - return false; - } -} diff --git a/app/Event/TaskNotificationListener.php b/app/Event/TaskNotificationListener.php deleted file mode 100644 index 119e61e5..00000000 --- a/app/Event/TaskNotificationListener.php +++ /dev/null @@ -1,29 +0,0 @@ -<?php - -namespace Event; - -use Event\BaseNotificationListener; - -/** - * Task notification listener - * - * @package event - * @author Frederic Guillot - */ -class TaskNotificationListener extends BaseNotificationListener -{ - /** - * Fetch data for the mail template - * - * @access public - * @param array $data Event data - * @return array - */ - public function getTemplateData(array $data) - { - $values = array(); - $values['task'] = $this->notification->taskFinder->getDetails($data['task_id']); - - return $values; - } -} diff --git a/app/Event/WebhookListener.php b/app/Event/WebhookListener.php index c2f6d56a..f7e23e07 100644 --- a/app/Event/WebhookListener.php +++ b/app/Event/WebhookListener.php @@ -2,26 +2,15 @@ namespace Event; -use Core\Listener; -use Model\Webhook; - /** * Webhook task events * * @package event * @author Frederic Guillot */ -class WebhookListener implements Listener +class WebhookListener extends Base { /** - * Webhook model - * - * @accesss private - * @var \Model\Webhook - */ - private $webhook; - - /** * Url to call * * @access private @@ -30,27 +19,14 @@ class WebhookListener implements Listener private $url = ''; /** - * Constructor + * Set webhook url * * @access public - * @param string $url URL to call - * @param \Model\Webhook $webhook Webhook model instance + * @param string $url URL to call */ - public function __construct($url, Webhook $webhook) + public function setUrl($url) { $this->url = $url; - $this->webhook = $webhook; - } - - /** - * Return class information - * - * @access public - * @return string - */ - public function __toString() - { - return get_called_class(); } /** |