summaryrefslogtreecommitdiff
path: root/app/Console/TaskTrigger.php
blob: 8d707211c74150ba32177256418287614b844601 (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
<?php

namespace Kanboard\Console;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Kanboard\Model\Task;
use Kanboard\Event\TaskListEvent;

class TaskTrigger extends Base
{
    protected function configure()
    {
        $this
            ->setName('trigger:tasks')
            ->setDescription('Trigger scheduler event for all tasks');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        foreach ($this->getProjectIds() as $project_id) {
            $tasks = $this->taskFinder->getAll($project_id);
            $nb_tasks = count($tasks);

            if ($nb_tasks > 0) {
                $output->writeln('Trigger task event: project_id='.$project_id.', nb_tasks='.$nb_tasks);
                $this->sendEvent($tasks, $project_id);
            }
        }
    }

    private function getProjectIds()
    {
        $listeners = $this->dispatcher->getListeners(Task::EVENT_DAILY_CRONJOB);
        $project_ids = array();

        foreach ($listeners as $listener) {
            $project_ids[] = $listener[0]->getProjectId();
        }

        return array_unique($project_ids);
    }

    private function sendEvent(array &$tasks, $project_id)
    {
        $event = new TaskListEvent(array('project_id' => $project_id));
        $event->setTasks($tasks);

        $this->dispatcher->dispatch(Task::EVENT_DAILY_CRONJOB, $event);
    }
}