Commit 106913cf authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

add reader action and controller

parent 31431e3c
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
<?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);
    }
}
+6 −7
Original line number Diff line number Diff line
<?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);
    }
}