From 074056352de98fc567b4d13184c72887c75625d0 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 12 Oct 2014 21:38:56 -0400 Subject: Project activity refactoring and listeners improvements --- app/Event/Base.php | 79 ++++++++++++++++++++++++ app/Event/BaseNotificationListener.php | 87 --------------------------- app/Event/CommentHistoryListener.php | 73 ---------------------- app/Event/CommentNotificationListener.php | 30 --------- app/Event/FileNotificationListener.php | 30 --------- app/Event/NotificationListener.php | 83 +++++++++++++++++++++++++ app/Event/ProjectActivityListener.php | 61 +++++++++++++++++++ app/Event/ProjectModificationDate.php | 63 ------------------- app/Event/ProjectModificationDateListener.php | 30 +++++++++ app/Event/SubTaskNotificationListener.php | 30 --------- app/Event/SubtaskHistoryListener.php | 73 ---------------------- app/Event/TaskHistoryListener.php | 63 ------------------- app/Event/TaskNotificationListener.php | 29 --------- app/Event/WebhookListener.php | 32 ++-------- 14 files changed, 257 insertions(+), 506 deletions(-) create mode 100644 app/Event/Base.php delete mode 100644 app/Event/BaseNotificationListener.php delete mode 100644 app/Event/CommentHistoryListener.php delete mode 100644 app/Event/CommentNotificationListener.php delete mode 100644 app/Event/FileNotificationListener.php create mode 100644 app/Event/NotificationListener.php create mode 100644 app/Event/ProjectActivityListener.php delete mode 100644 app/Event/ProjectModificationDate.php create mode 100644 app/Event/ProjectModificationDateListener.php delete mode 100644 app/Event/SubTaskNotificationListener.php delete mode 100644 app/Event/SubtaskHistoryListener.php delete mode 100644 app/Event/TaskHistoryListener.php delete mode 100644 app/Event/TaskNotificationListener.php (limited to 'app/Event') 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 @@ +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 @@ -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 @@ -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 @@ -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 @@ -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 @@ +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 @@ +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 @@ -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 @@ +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 @@ -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 @@ -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 @@ -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 @@ -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,25 +2,14 @@ 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 * @@ -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(); } /** -- cgit v1.2.3