. */ namespace SP\Modules\Api\Controllers\Config; use Exception; use Klein\Klein; use SP\Core\Acl\Acl; use SP\Core\Application; use SP\Core\Events\Event; use SP\Core\Events\EventMessage; use SP\Domain\Api\Ports\ApiServiceInterface; use SP\Domain\Api\Services\ApiResponse; use SP\Domain\Core\Acl\AclActionsInterface; use SP\Domain\Core\Acl\AclInterface; use SP\Domain\Core\Exceptions\InvalidClassException; use SP\Domain\Export\Ports\FileBackupServiceInterface; use SP\Domain\Export\Services\BackupFiles; use SP\Modules\Api\Controllers\ControllerBase; use SP\Modules\Api\Controllers\Help\ConfigHelp; /** * Class BackupController * * @package SP\Modules\Api\Controllers */ final class BackupController extends ControllerBase { private FileBackupServiceInterface $fileBackupService; /** * @throws InvalidClassException */ public function __construct( Application $application, Klein $router, ApiServiceInterface $apiService, AclInterface $acl, FileBackupServiceInterface $fileBackupService ) { parent::__construct($application, $router, $apiService, $acl); $this->fileBackupService = $fileBackupService; $this->apiService->setHelpClass(ConfigHelp::class); } /** * backupAction */ public function backupAction(): void { try { $this->setupApi(AclActionsInterface::CONFIG_BACKUP_RUN); $path = $this->apiService->getParamString('path', false, BACKUP_PATH); $this->fileBackupService->doBackup($path); $this->eventDispatcher->notify( 'run.backup.end', new Event( $this, EventMessage::factory() ->addDescription(__u('Application and database backup completed successfully')) ->addDetail(__u('Path'), $path) ) ); $this->returnResponse( ApiResponse::makeSuccess($this->buildBackupFiles($path), null, __('Backup process finished')) ); } catch (Exception $e) { processException($e); $this->returnResponseException($e); } } /** * @param string|null $path * * @return array[] */ private function buildBackupFiles(?string $path): array { return [ 'files' => [ 'app' => BackupFiles::getAppBackupFilename( $path, $this->fileBackupService->getHash(), true ), 'db' => BackupFiles::getDbBackupFilename( $path, $this->fileBackupService->getHash(), true ), ], ]; } }