Passed
Push — 3.0 ( 8cdcee...90ba03 )
by Rubén
07:01
created

TaskController::trackStatusAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 1
dl 0
loc 20
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use Klein\Klein;
28
use Psr\Container\ContainerInterface;
29
use SP\Services\ServiceException;
30
use SP\Services\Task\TaskFactory;
31
use SP\Services\Task\TaskService;
32
33
/**
34
 * Class TaskController
35
 *
36
 * @package SP\Modules\Web\Controllers
37
 */
38
final class TaskController
39
{
40
    /**
41
     * @var TaskService
42
     */
43
    private $taskService;
44
    /**
45
     * @var Klein
46
     */
47
    private $router;
48
49
    /**
50
     * TaskController constructor.
51
     *
52
     * @param ContainerInterface $container
53
     */
54
    public function __construct(ContainerInterface $container)
55
    {
56
        $this->router = $container->get(Klein::class);
57
        $this->taskService = $container->get(TaskService::class);
58
    }
59
60
    /**
61
     * @param string $taskId
62
     */
63
    public function trackStatusAction($taskId)
64
    {
65
        $response = $this->router->response();
66
        $response->header('Content-Type', 'text/event-stream');
67
        $response->header('Cache-Control', 'no-store, no-cache');
68
        $response->header('Access-Control-Allow-Origin', '*');
69
        $response->send(true);
70
71
        ob_end_flush();
72
73
        try {
74
            $this->taskService->trackStatus($taskId,
75
                function ($id, $message) {
76
                    echo 'id: ', $id, PHP_EOL, 'data: ', $message, PHP_EOL, PHP_EOL;
77
78
                    ob_flush();
79
                    flush();
80
                });
81
        } catch (ServiceException $e) {
82
            processException($e);
83
        }
84
    }
85
86
    /**
87
     * @param $taskId
88
     *
89
     * @throws \SP\Storage\File\FileException
90
     */
91
    public function testTaskAction($taskId)
92
    {
93
        $task = TaskFactory::create($taskId, $taskId);
94
95
        echo $task->getTaskId();
96
97
        $count = 0;
98
99
        while ($count < 60) {
100
            TaskFactory::update($task,
101
                TaskFactory::createMessage($task->getTaskId(), "Test Task $count")
102
            );
103
104
            sleep(1);
105
            $count++;
106
        }
107
108
        TaskFactory::end($task);
109
    }
110
}