diff --git a/src/Actions/ReadLogFileAction.php b/src/Actions/ReadLogFileAction.php new file mode 100644 index 0000000000000000000000000000000000000000..ba63dd798f6508def3c58b48b1fbff4ed593adb2 --- /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 f62e6a214bbb620b426808af83540eabdc19c3f4..263ab111f6075364ef9e8b115378e312ebadafbf 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); } }