summaryrefslogtreecommitdiff
path: root/app/Job/TaskEventJob.php
blob: 7d026a6824dea7e4802adf0eafc67c9a500f0636 (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
<?php

namespace Kanboard\Job;

use Kanboard\Event\TaskEvent;
use Kanboard\EventBuilder\TaskEventBuilder;
use Kanboard\Model\TaskModel;

/**
 * Class TaskEventJob
 *
 * @package Kanboard\Job
 * @author  Frederic Guillot
 */
class TaskEventJob extends BaseJob
{
    /**
     * Set job params
     *
     * @param  int    $taskId
     * @param  array  $eventNames
     * @param  array  $changes
     * @param  array  $values
     * @param  array  $task
     * @return $this
     */
    public function withParams($taskId, array $eventNames, array $changes = array(), array $values = array(), array $task = array())
    {
        $this->jobParams = array($taskId, $eventNames, $changes, $values, $task);
        return $this;
    }

    /**
     * Execute job
     *
     * @param  int    $taskId
     * @param  array  $eventNames
     * @param  array  $changes
     * @param  array  $values
     * @param  array  $task
     * @return $this
     */
    public function execute($taskId, array $eventNames, array $changes = array(), array $values = array(), array $task = array())
    {
        $event = TaskEventBuilder::getInstance($this->container)
            ->withTaskId($taskId)
            ->withChanges($changes)
            ->withValues($values)
            ->withTask($task)
            ->buildEvent();

        if ($event !== null) {
            foreach ($eventNames as $eventName) {
                $this->fireEvent($eventName, $event);
            }
        }
    }

    /**
     * Trigger event
     *
     * @access protected
     * @param  string    $eventName
     * @param  TaskEvent $event
     */
    protected function fireEvent($eventName, TaskEvent $event)
    {
        $this->logger->debug(__METHOD__.' Event fired: '.$eventName);
        $this->dispatcher->dispatch($eventName, $event);

        if ($eventName === TaskModel::EVENT_CREATE) {
            $this->userMentionModel->fireEvents($event['task']['description'], TaskModel::EVENT_USER_MENTION, $event);
        }
    }
}