diff --git a/app/modules/web/Controllers/Task/TestTaskController.php b/app/modules/web/Controllers/Task/TestTaskController.php new file mode 100644 index 00000000..cde8adff --- /dev/null +++ b/app/modules/web/Controllers/Task/TestTaskController.php @@ -0,0 +1,58 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Task; + + +use SP\Domain\Task\Services\TaskFactory; +use SP\Infrastructure\File\FileException; + +/** + * Class TestTaskController + */ +final class TestTaskController +{ + /** + * @param string $taskId + * + * @throws FileException + */ + public function testTaskAction(string $taskId): void + { + $task = TaskFactory::create($taskId, $taskId); + + echo $task->getTaskId(); + + $count = 0; + + while ($count < 60) { + TaskFactory::update($task, TaskFactory::createMessage($task->getTaskId(), "Test Task $count")); + + sleep(1); + $count++; + } + + TaskFactory::end($task); + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/Task/TrackStatusController.php b/app/modules/web/Controllers/Task/TrackStatusController.php new file mode 100644 index 00000000..88116e5b --- /dev/null +++ b/app/modules/web/Controllers/Task/TrackStatusController.php @@ -0,0 +1,75 @@ +. + */ + +namespace SP\Modules\Web\Controllers\Task; + +use Klein\Response; +use SP\Domain\Common\Services\ServiceException; +use SP\Domain\Task\TaskServiceInterface; + +/** + * Class TrackStatusController + * + * @package SP\Modules\Web\Controllers + */ +final class TrackStatusController +{ + private TaskServiceInterface $taskService; + private Response $response; + + public function __construct(Response $response, TaskServiceInterface $taskService) + { + $this->response = $response; + $this->taskService = $taskService; + } + + /** + * @param string $taskId + * + * @throws \JsonException + */ + public function trackStatusAction(string $taskId): void + { + $this->response->header('Content-Type', 'text/event-stream'); + $this->response->header('Cache-Control', 'no-store, no-cache'); + $this->response->header('Access-Control-Allow-Origin', '*'); + $this->response->send(true); + + ob_end_flush(); + + try { + $this->taskService->trackStatus( + $taskId, + function ($id, $message) { + echo 'id: ', $id, PHP_EOL, 'data: ', $message, PHP_EOL, PHP_EOL; + + ob_flush(); + flush(); + } + ); + } catch (ServiceException $e) { + processException($e); + } + } +} \ No newline at end of file diff --git a/app/modules/web/Controllers/TaskController.php b/app/modules/web/Controllers/TaskController.php deleted file mode 100644 index fcc7cb2d..00000000 --- a/app/modules/web/Controllers/TaskController.php +++ /dev/null @@ -1,117 +0,0 @@ -. - */ - -namespace SP\Modules\Web\Controllers; - -use Klein\Klein; -use Psr\Container\ContainerInterface; -use SP\Domain\Common\Services\ServiceException; -use SP\Domain\Task\Services\TaskFactory; -use SP\Domain\Task\Services\TaskService; -use SP\Domain\Task\TaskServiceInterface; -use SP\Infrastructure\File\FileException; - -/** - * Class TaskController - * - * @package SP\Modules\Web\Controllers - */ -final class TaskController -{ - /** - * @var TaskServiceInterface - */ - private $taskService; - /** - * @var Klein - */ - private $router; - - /** - * TaskController constructor. - * - * @param ContainerInterface $container - */ - public function __construct(ContainerInterface $container) - { - $this->router = $container->get(Klein::class); - $this->taskService = $container->get(TaskService::class); - } - - /** - * @param string $taskId - * - * @throws \JsonException - */ - public function trackStatusAction(string $taskId): void - { - $response = $this->router->response(); - $response->header('Content-Type', 'text/event-stream'); - $response->header('Cache-Control', 'no-store, no-cache'); - $response->header('Access-Control-Allow-Origin', '*'); - $response->send(true); - - ob_end_flush(); - - try { - $this->taskService->trackStatus($taskId, - function ($id, $message) { - echo 'id: ', $id, PHP_EOL, 'data: ', $message, PHP_EOL, PHP_EOL; - - ob_flush(); - flush(); - }); - } catch (ServiceException $e) { - processException($e); - } - } - - /** - * @param string $taskId - * - * @throws FileException - */ - public function testTaskAction(string $taskId): void - { - $task = TaskFactory::create($taskId, $taskId); - - echo $task->getTaskId(); - - $count = 0; - - while ($count < 60) { - TaskFactory::update($task, - TaskFactory::createMessage( - $task->getTaskId(), - "Test Task $count" - ) - ); - - sleep(1); - $count++; - } - - TaskFactory::end($task); - } -} \ No newline at end of file