From 106913cf399e003f9bf1c52679a10eb946389e08 Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" <a.plohih@iqdev.digital> Date: Tue, 16 Apr 2024 04:30:36 +0500 Subject: [PATCH] add reader action and controller --- src/Actions/ReadLogFileAction.php | 26 ++++++++++++++++++++++++ src/Controller/ReadLogFileController.php | 13 ++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/Actions/ReadLogFileAction.php diff --git a/src/Actions/ReadLogFileAction.php b/src/Actions/ReadLogFileAction.php new file mode 100644 index 0000000..ba63dd7 --- /dev/null +++ b/src/Actions/ReadLogFileAction.php @@ -0,0 +1,26 @@ +<?php + +namespace App\Actions; + +use Exception; + +class ReadLogFileAction +{ + /** + * Принимает путь до файла, + * проверÑет, что файл ÑущеÑтвует и выводит пользователю веÑÑŒ контент файла + * (файл можешь Ñоздать любой) + * + * @param string $filePath путь до файла + * @return false|string + * @throws Exception при отÑутÑвии фаила по переданному пути + */ + public function act(string $filePath) + { + if (!file_exists($filePath)) { + throw new Exception("По данному пути ничего не найдено: $filePath"); + } + + return file_get_contents($filePath); + } +} diff --git a/src/Controller/ReadLogFileController.php b/src/Controller/ReadLogFileController.php index f62e6a2..263ab11 100644 --- a/src/Controller/ReadLogFileController.php +++ b/src/Controller/ReadLogFileController.php @@ -1,8 +1,8 @@ <?php namespace App\Controller; +use App\Actions\ReadLogFileAction; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; -use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; @@ -16,13 +16,12 @@ class ReadLogFileController extends AbstractController * @param string $filePath путь до файла * @return Response */ #[Route('/read/{filePath}', name: 'app_read_log_file', methods: ['GET'])] - public function index(string $filePath): Response + public function index(string $filePath, ReadLogFileAction $action): Response { - $fileSystem = new Filesystem(); - if (!$fileSystem->exists($filePath)) { - return new Response('File not found', Response::HTTP_NOT_FOUND); + try { + return new Response($action->act($filePath)); + } catch (\Exception $exception) { + return new Response('Searching file not found :/', 422); } - - return new Response(file_get_contents($filePath), Response::HTTP_OK); } } -- GitLab