summaryrefslogtreecommitdiff
path: root/app/Subscriber/ProjectActivitySubscriber.php
blob: 42314637fd8534923a5a3e0e60747ee580fe47ad (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
81
82
83
84
85
86
87
88
89
90
<?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, $event_name)
    {
        // Executed only when someone is logged
        if ($this->userSession->isLogged() && isset($event['task_id'])) {

            $values = $this->getValues($event);

            $this->projectActivity->createEvent(
                $values['task']['project_id'],
                $values['task']['id'],
                $this->userSession->getId(),
                $event_name,
                $values
            );

            $this->sendSlackNotification($event_name, $values);
            $this->sendHipchatNotification($event_name, $values);
        }
    }

    private function sendSlackNotification($event_name, array $values)
    {
        if ($this->config->get('integration_slack_webhook') == 1) {
            $this->slackWebhook->notify(
                $values['task']['project_id'],
                $values['task']['id'],
                $event_name,
                $values
            );
        }
    }

    private function sendHipchatNotification($event_name, array $values)
    {
        if ($this->config->get('integration_hipchat') == 1) {
            $this->hipchat->notify(
                $values['task']['project_id'],
                $values['task']['id'],
                $event_name,
                $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;
    }
}