summaryrefslogtreecommitdiff
path: root/app/Subscriber
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2014-12-27 19:10:38 -0500
committerFrédéric Guillot <fred@kanboard.net>2014-12-27 19:10:38 -0500
commit17dc5bdc9ede52ad618bbf326e67e3b6988170f7 (patch)
tree9cf4d325667f11fa735bca84042fb385e3273329 /app/Subscriber
parentcf821e117ce8b937cff7f386a107aaa81ba6bf9b (diff)
Move events handling to Symfony\EventDispatcher
Diffstat (limited to 'app/Subscriber')
-rw-r--r--app/Subscriber/Base.php47
-rw-r--r--app/Subscriber/NotificationSubscriber.php81
-rw-r--r--app/Subscriber/ProjectActivitySubscriber.php63
-rw-r--r--app/Subscriber/ProjectDailySummarySubscriber.php27
-rw-r--r--app/Subscriber/ProjectModificationDateSubscriber.php38
-rw-r--r--app/Subscriber/WebhookSubscriber.php42
6 files changed, 298 insertions, 0 deletions
diff --git a/app/Subscriber/Base.php b/app/Subscriber/Base.php
new file mode 100644
index 00000000..1ed15327
--- /dev/null
+++ b/app/Subscriber/Base.php
@@ -0,0 +1,47 @@
+<?php
+
+namespace Subscriber;
+
+use Event\TaskEvent;
+use Model\Task;
+use Pimple\Container;
+
+/**
+ * Base subscriber class
+ *
+ * @package subscriber
+ * @author Frederic Guillot
+ */
+abstract class Base
+{
+ /**
+ * Container instance
+ *
+ * @access protected
+ * @var \Pimple\Container
+ */
+ protected $container;
+
+ /**
+ * Constructor
+ *
+ * @access public
+ * @param \Pimple\Container $container
+ */
+ public function __construct(Container $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * Load automatically models
+ *
+ * @access public
+ * @param string $name Model name
+ * @return mixed
+ */
+ public function __get($name)
+ {
+ return $this->container[$name];
+ }
+}
diff --git a/app/Subscriber/NotificationSubscriber.php b/app/Subscriber/NotificationSubscriber.php
new file mode 100644
index 00000000..4412636f
--- /dev/null
+++ b/app/Subscriber/NotificationSubscriber.php
@@ -0,0 +1,81 @@
+<?php
+
+namespace Subscriber;
+
+use Event\GenericEvent;
+use Model\Task;
+use Model\Comment;
+use Model\SubTask;
+use Model\File;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class NotificationSubscriber extends Base implements EventSubscriberInterface
+{
+ private $templates = array(
+ Task::EVENT_CREATE => 'task_creation',
+ Task::EVENT_UPDATE => 'task_update',
+ Task::EVENT_CLOSE => 'task_close',
+ Task::EVENT_OPEN => 'task_open',
+ Task::EVENT_MOVE_COLUMN => 'task_move_column',
+ Task::EVENT_MOVE_POSITION => 'task_move_position',
+ Task::EVENT_ASSIGNEE_CHANGE => 'task_assignee_change',
+ SubTask::EVENT_CREATE => 'subtask_creation',
+ SubTask::EVENT_UPDATE => 'subtask_update',
+ Comment::EVENT_CREATE => 'comment_creation',
+ Comment::EVENT_UPDATE => 'comment_update',
+ File::EVENT_CREATE => 'file_creation',
+ );
+
+ public static function getSubscribedEvents()
+ {
+ return array(
+ Task::EVENT_CREATE => array('execute', 0),
+ Task::EVENT_UPDATE => array('execute', 0),
+ Task::EVENT_CLOSE => array('execute', 0),
+ Task::EVENT_OPEN => array('execute', 0),
+ Task::EVENT_MOVE_COLUMN => array('execute', 0),
+ Task::EVENT_MOVE_POSITION => array('execute', 0),
+ Task::EVENT_ASSIGNEE_CHANGE => array('execute', 0),
+ SubTask::EVENT_CREATE => array('execute', 0),
+ SubTask::EVENT_UPDATE => array('execute', 0),
+ Comment::EVENT_CREATE => array('execute', 0),
+ Comment::EVENT_UPDATE => array('execute', 0),
+ File::EVENT_CREATE => array('execute', 0),
+ );
+ }
+
+ public function execute(GenericEvent $event)
+ {
+ $values = $this->getTemplateData($event);
+ $users = $this->notification->getUsersList($values['task']['project_id']);
+
+ if ($users) {
+ $this->notification->sendEmails($this->templates[$event->getName()], $users, $values);
+ }
+ }
+
+ public function getTemplateData(GenericEvent $event)
+ {
+ $values = array();
+
+ switch (get_class($event)) {
+ case 'Event\TaskEvent':
+ $values['task'] = $this->taskFinder->getDetails($event['task_id']);
+ break;
+ case 'Event\SubtaskEvent':
+ $values['subtask'] = $this->subTask->getById($event['id'], true);
+ $values['task'] = $this->taskFinder->getDetails($event['task_id']);
+ break;
+ case 'Event\FileEvent':
+ $values['file'] = $event->getAll();
+ $values['task'] = $this->taskFinder->getDetails($event['task_id']);
+ break;
+ case 'Event\CommentEvent':
+ $values['comment'] = $this->comment->getById($event['id']);
+ $values['task'] = $this->taskFinder->getDetails($values['comment']['task_id']);
+ break;
+ }
+
+ return $values;
+ }
+}
diff --git a/app/Subscriber/ProjectActivitySubscriber.php b/app/Subscriber/ProjectActivitySubscriber.php
new file mode 100644
index 00000000..1aca8539
--- /dev/null
+++ b/app/Subscriber/ProjectActivitySubscriber.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Subscriber;
+
+use Event\GenericEvent;
+use Model\Task;
+use Model\Comment;
+use Model\SubTask;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class ProjectActivitySubscriber extends Base implements EventSubscriberInterface
+{
+ public static function getSubscribedEvents()
+ {
+ return array(
+ Task::EVENT_ASSIGNEE_CHANGE => array('execute', 0),
+ Task::EVENT_UPDATE => array('execute', 0),
+ Task::EVENT_CREATE => array('execute', 0),
+ Task::EVENT_CLOSE => array('execute', 0),
+ Task::EVENT_OPEN => array('execute', 0),
+ Task::EVENT_MOVE_COLUMN => array('execute', 0),
+ Task::EVENT_MOVE_POSITION => array('execute', 0),
+ Comment::EVENT_UPDATE => array('execute', 0),
+ Comment::EVENT_CREATE => array('execute', 0),
+ SubTask::EVENT_UPDATE => array('execute', 0),
+ SubTask::EVENT_CREATE => array('execute', 0),
+ );
+ }
+
+ public function execute(GenericEvent $event)
+ {
+ // Executed only when someone is logged
+ if ($this->container['acl']->isLogged() && isset($event['task_id'])) {
+
+ $values = $this->getValues($event);
+
+ $this->projectActivity->createEvent(
+ $values['task']['project_id'],
+ $values['task']['id'],
+ $this->acl->getUserId(),
+ $event->getName(),
+ $values
+ );
+ }
+ }
+
+ private function getValues(GenericEvent $event)
+ {
+ $values = array();
+ $values['task'] = $this->taskFinder->getDetails($event['task_id']);
+
+ switch (get_class($event)) {
+ case 'Event\SubtaskEvent':
+ $values['subtask'] = $this->subTask->getById($event['id'], true);
+ break;
+ case 'Event\CommentEvent':
+ $values['comment'] = $this->comment->getById($event['id']);
+ break;
+ }
+
+ return $values;
+ }
+}
diff --git a/app/Subscriber/ProjectDailySummarySubscriber.php b/app/Subscriber/ProjectDailySummarySubscriber.php
new file mode 100644
index 00000000..6d737734
--- /dev/null
+++ b/app/Subscriber/ProjectDailySummarySubscriber.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Subscriber;
+
+use Event\TaskEvent;
+use Model\Task;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class ProjectDailySummarySubscriber extends Base implements EventSubscriberInterface
+{
+ public static function getSubscribedEvents()
+ {
+ return array(
+ Task::EVENT_CREATE => array('execute', 0),
+ Task::EVENT_CLOSE => array('execute', 0),
+ Task::EVENT_OPEN => array('execute', 0),
+ Task::EVENT_MOVE_COLUMN => array('execute', 0),
+ );
+ }
+
+ public function execute(TaskEvent $event)
+ {
+ if (isset($event['project_id'])) {
+ $this->projectDailySummary->updateTotals($event['project_id'], date('Y-m-d'));
+ }
+ }
+}
diff --git a/app/Subscriber/ProjectModificationDateSubscriber.php b/app/Subscriber/ProjectModificationDateSubscriber.php
new file mode 100644
index 00000000..3d5484f7
--- /dev/null
+++ b/app/Subscriber/ProjectModificationDateSubscriber.php
@@ -0,0 +1,38 @@
+<?php
+
+namespace Subscriber;
+
+use Event\GenericEvent;
+use Model\Task;
+use Model\GithubWebhook;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class ProjectModificationDateSubscriber extends Base implements EventSubscriberInterface
+{
+ public static function getSubscribedEvents()
+ {
+ return array(
+ Task::EVENT_CREATE_UPDATE => array('execute', 0),
+ Task::EVENT_CLOSE => array('execute', 0),
+ Task::EVENT_OPEN => array('execute', 0),
+ Task::EVENT_MOVE_COLUMN => array('execute', 0),
+ Task::EVENT_MOVE_POSITION => array('execute', 0),
+ Task::EVENT_MOVE_PROJECT => array('execute', 0),
+ Task::EVENT_ASSIGNEE_CHANGE => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_OPENED => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_CLOSED => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_REOPENED => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_ASSIGNEE_CHANGE => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_LABEL_CHANGE => array('execute', 0),
+ GithubWebhook::EVENT_ISSUE_COMMENT => array('execute', 0),
+ GithubWebhook::EVENT_COMMIT => array('execute', 0),
+ );
+ }
+
+ public function execute(GenericEvent $event)
+ {
+ if (isset($event['project_id'])) {
+ $this->project->updateModificationDate($event['project_id']);
+ }
+ }
+}
diff --git a/app/Subscriber/WebhookSubscriber.php b/app/Subscriber/WebhookSubscriber.php
new file mode 100644
index 00000000..20d765e2
--- /dev/null
+++ b/app/Subscriber/WebhookSubscriber.php
@@ -0,0 +1,42 @@
+<?php
+
+namespace Subscriber;
+
+use Event\TaskEvent;
+use Model\Task;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+class WebhookSubscriber extends Base implements EventSubscriberInterface
+{
+ public static function getSubscribedEvents()
+ {
+ return array(
+ Task::EVENT_CREATE => array('onTaskCreation', 0),
+ Task::EVENT_UPDATE => array('onTaskModification', 0),
+ Task::EVENT_CLOSE => array('onTaskModification', 0),
+ Task::EVENT_OPEN => array('onTaskModification', 0),
+ Task::EVENT_MOVE_COLUMN => array('onTaskModification', 0),
+ Task::EVENT_MOVE_POSITION => array('onTaskModification', 0),
+ Task::EVENT_ASSIGNEE_CHANGE => array('onTaskModification', 0),
+ );
+ }
+
+ public function onTaskCreation(TaskEvent $event)
+ {
+ $this->executeRequest('webhook_url_task_creation');
+ }
+
+ public function onTaskModification(TaskEvent $event)
+ {
+ $this->executeRequest('webhook_url_task_modification');
+ }
+
+ public function executeRequest($parameter)
+ {
+ $url = $this->config->get($parameter);
+
+ if (! empty($url)) {
+ $this->webhook->notify($url, $event->getAll());
+ }
+ }
+}