diff options
author | Frederic Guillot <fred@kanboard.net> | 2016-05-24 20:30:31 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2016-05-24 20:30:31 -0400 |
commit | b9f2d5650d464983a80f84f3ed0845028a355c92 (patch) | |
tree | 36ab3e69b0b2c3061746a27dbf2a5c2a3c6ce89a /app | |
parent | cbee789549c7fde02f52061e19fe96dbf667c64a (diff) |
Added NotificationJob
Diffstat (limited to 'app')
-rw-r--r-- | app/Job/NotificationJob.php | 85 | ||||
-rw-r--r-- | app/Subscriber/NotificationSubscriber.php | 74 |
2 files changed, 105 insertions, 54 deletions
diff --git a/app/Job/NotificationJob.php b/app/Job/NotificationJob.php new file mode 100644 index 00000000..d5fce222 --- /dev/null +++ b/app/Job/NotificationJob.php @@ -0,0 +1,85 @@ +<?php + +namespace Kanboard\Job; + +use Kanboard\Event\GenericEvent; + +/** + * Class NotificationJob + * + * @package Kanboard\Job + * @author Frederic Guillot + */ +class NotificationJob extends BaseJob +{ + /** + * Set job parameters + * + * @param GenericEvent $event + * @param string $eventName + * @param string $eventObjectName + * @return $this + */ + public function withParams(GenericEvent $event, $eventName, $eventObjectName) + { + $this->jobParams = array($event->getAll(), $eventName, $eventObjectName); + return $this; + } + + /** + * Execute job + * + * @param array $event + * @param string $eventName + * @param string $eventObjectName + */ + public function execute(array $event, $eventName, $eventObjectName) + { + $eventData = $this->getEventData($event, $eventObjectName); + + if (! empty($eventData)) { + if (! empty($event['mention'])) { + $this->userNotification->sendUserNotification($event['mention'], $eventName, $eventData); + } else { + $this->userNotification->sendNotifications($eventName, $eventData); + $this->projectNotification->sendNotifications($eventData['task']['project_id'], $eventName, $eventData); + } + } + } + + /** + * Get event data + * + * @param array $event + * @param string $eventObjectName + * @return array + */ + public function getEventData(array $event, $eventObjectName) + { + $values = array(); + + if (! empty($event['changes'])) { + $values['changes'] = $event['changes']; + } + + switch ($eventObjectName) { + case 'Kanboard\Event\TaskEvent': + $values['task'] = $this->taskFinder->getDetails($event['task_id']); + break; + case 'Kanboard\Event\SubtaskEvent': + $values['subtask'] = $this->subtask->getById($event['id'], true); + $values['task'] = $this->taskFinder->getDetails($values['subtask']['task_id']); + break; + case 'Kanboard\Event\FileEvent': + $values['file'] = $event; + $values['task'] = $this->taskFinder->getDetails($values['file']['task_id']); + break; + case 'Kanboard\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/NotificationSubscriber.php b/app/Subscriber/NotificationSubscriber.php index 651b8a96..2a1fe6c7 100644 --- a/app/Subscriber/NotificationSubscriber.php +++ b/app/Subscriber/NotificationSubscriber.php @@ -3,6 +3,7 @@ namespace Kanboard\Subscriber; use Kanboard\Event\GenericEvent; +use Kanboard\Job\NotificationJob; use Kanboard\Model\Task; use Kanboard\Model\Comment; use Kanboard\Model\Subtask; @@ -14,67 +15,32 @@ class NotificationSubscriber extends BaseSubscriber implements EventSubscriberIn public static function getSubscribedEvents() { return array( - Task::EVENT_USER_MENTION => 'handleEvent', - Task::EVENT_CREATE => 'handleEvent', - Task::EVENT_UPDATE => 'handleEvent', - Task::EVENT_CLOSE => 'handleEvent', - Task::EVENT_OPEN => 'handleEvent', - Task::EVENT_MOVE_COLUMN => 'handleEvent', - Task::EVENT_MOVE_POSITION => 'handleEvent', - Task::EVENT_MOVE_SWIMLANE => 'handleEvent', + Task::EVENT_USER_MENTION => 'handleEvent', + Task::EVENT_CREATE => 'handleEvent', + Task::EVENT_UPDATE => 'handleEvent', + Task::EVENT_CLOSE => 'handleEvent', + Task::EVENT_OPEN => 'handleEvent', + Task::EVENT_MOVE_COLUMN => 'handleEvent', + Task::EVENT_MOVE_POSITION => 'handleEvent', + Task::EVENT_MOVE_SWIMLANE => 'handleEvent', Task::EVENT_ASSIGNEE_CHANGE => 'handleEvent', - Subtask::EVENT_CREATE => 'handleEvent', - Subtask::EVENT_UPDATE => 'handleEvent', - Comment::EVENT_CREATE => 'handleEvent', - Comment::EVENT_UPDATE => 'handleEvent', + Subtask::EVENT_CREATE => 'handleEvent', + Subtask::EVENT_UPDATE => 'handleEvent', + Comment::EVENT_CREATE => 'handleEvent', + Comment::EVENT_UPDATE => 'handleEvent', Comment::EVENT_USER_MENTION => 'handleEvent', - TaskFile::EVENT_CREATE => 'handleEvent', + TaskFile::EVENT_CREATE => 'handleEvent', ); } - public function handleEvent(GenericEvent $event, $event_name) + public function handleEvent(GenericEvent $event, $eventName) { - if (! $this->isExecuted($event_name)) { - $this->logger->debug('Subscriber executed: '.__METHOD__); - $event_data = $this->getEventData($event); + if (!$this->isExecuted($eventName)) { + $this->logger->debug('Subscriber executed: ' . __METHOD__); - if (! empty($event_data)) { - if (! empty($event['mention'])) { - $this->userNotification->sendUserNotification($event['mention'], $event_name, $event_data); - } else { - $this->userNotification->sendNotifications($event_name, $event_data); - $this->projectNotification->sendNotifications($event_data['task']['project_id'], $event_name, $event_data); - } - } + $this->queueManager->push(NotificationJob::getInstance($this->container) + ->withParams($event, $eventName, get_class($event)) + ); } } - - public function getEventData(GenericEvent $event) - { - $values = array(); - - if (! empty($event['changes'])) { - $values['changes'] = $event['changes']; - } - - switch (get_class($event)) { - case 'Kanboard\Event\TaskEvent': - $values['task'] = $this->taskFinder->getDetails($event['task_id']); - break; - case 'Kanboard\Event\SubtaskEvent': - $values['subtask'] = $this->subtask->getById($event['id'], true); - $values['task'] = $this->taskFinder->getDetails($values['subtask']['task_id']); - break; - case 'Kanboard\Event\FileEvent': - $values['file'] = $event->getAll(); - $values['task'] = $this->taskFinder->getDetails($values['file']['task_id']); - break; - case 'Kanboard\Event\CommentEvent': - $values['comment'] = $this->comment->getById($event['id']); - $values['task'] = $this->taskFinder->getDetails($values['comment']['task_id']); - break; - } - - return $values; - } } |