BackupRestoreController.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 5

Versionen 11

Code

<?php

declare(strict_types=1);

namespace Controller;

// @responsibility: HTTP-Endpunkte für Datei-Backup und -Wiederherstellung

use Domain\Repository\FileBackupRepositoryInterface;
use Framework\Controller;

class BackupRestoreController extends Controller
{
    private FileBackupRepositoryInterface $repository;

    public function __construct(FileBackupRepositoryInterface $repository)
    {
        $this->repository = $repository;
    }

    /**
     * GET /backup-restore
     * List all backups.
     */
    public function index(): void
    {
        $filters = [];

        $search = $this->getString('search');
        if ($search !== '') {
            $filters['search'] = $search;
        }

        $changeType = $this->getString('change_type');
        if ($changeType !== '') {
            $filters['change_type'] = $changeType;
        }

        $limit = 50;
        $offset = $this->getInt('offset');

        $backups = $this->repository->findAll($filters, $limit, $offset);
        $total = $this->repository->count($filters);
        $stats = $this->repository->getStatistics();

        $this->view('backup-restore.index', [
            'title' => 'File Backup',
            'backups' => $backups,
            'stats' => $stats,
            'total' => $total,
            'filters' => $filters,
        ]);
    }

    /**
     * GET /backup-restore/{id}
     * Show backup details.
     */
    public function show(string $id): void
    {
        $backup = $this->repository->findById((int) $id);

        if ($backup === null) {
            $this->notFound('Backup nicht gefunden');
        }

        // Get content preview (max 500 lines)
        $contentPreview = $this->repository->getContentPreview((int) $id, 500);

        // Get other versions of this file
        $versions = $this->repository->findByFilePath($backup['file_path']);

        // Check if file currently exists
        $fileExists = file_exists($backup['file_path']);

        $this->view('backup-restore.show', [
            'title' => 'Backup #' . $id,
            'backup' => $backup,
            'contentPreview' => $contentPreview,
            'versions' => $versions,
            'fileExists' => $fileExists,
        ]);
    }

    /**
     * POST /backup-restore/{id}/restore
     * Restore file from backup.
     */
    public function restore(string $id): void
    {
        $this->requireCsrf();

        $isHtmx = $this->isHtmxRequest();

        try {
            $this->repository->restore((int) $id);

            if ($isHtmx) {
                $this->htmxRedirect('/backup-restore/' . $id . '?restored=1');

                return;
            }

            $this->redirect('/backup-restore/' . $id . '?restored=1');
        } catch (\RuntimeException $e) {
            if ($isHtmx) {
                $this->htmxError($e->getMessage());

                return;
            }

            $this->redirect('/backup-restore/' . $id . '?error=' . urlencode($e->getMessage()));
        }
    }

    /**
     * GET /backup-restore/{id}/download
     * Download full backup content as file.
     */
    public function downloadBackup(string $id): void
    {
        $backup = $this->repository->findById((int) $id);

        if ($backup === null) {
            $this->notFound('Backup nicht gefunden');
        }

        $filename = basename($backup['file_path']) . '.backup-v' . $backup['version'];
        $this->download($backup['file_content'], $filename, 'text/plain');
    }
}
← Übersicht Graph