summaryrefslogtreecommitdiff
path: root/app/Subscriber/NotificationSubscriber.php
blob: 076600506c358611777e0993b19713a6b4ab4556 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

namespace Kanboard\Subscriber;

use Kanboard\Event\GenericEvent;
use Kanboard\Model\Task;
use Kanboard\Model\Comment;
use Kanboard\Model\Subtask;
use Kanboard\Model\File;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class NotificationSubscriber extends BaseSubscriber implements EventSubscriberInterface
{
    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_ASSIGNEE_CHANGE => 'handleEvent',
            Subtask::EVENT_CREATE => 'handleEvent',
            Subtask::EVENT_UPDATE => 'handleEvent',
            Comment::EVENT_CREATE => 'handleEvent',
            Comment::EVENT_UPDATE => 'handleEvent',
            Comment::EVENT_USER_MENTION => 'handleEvent',
            File::EVENT_CREATE => 'handleEvent',
        );
    }

    public function handleEvent(GenericEvent $event, $event_name)
    {
        if (! $this->isExecuted($event_name)) {
            $this->logger->debug('Subscriber executed: '.__METHOD__);
            $event_data = $this->getEventData($event);

            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);
                }
            }
        }
    }

    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;
    }
}