Skip to content

Commit

Permalink
ref: refactor controller (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot authored Apr 14, 2024
1 parent a040641 commit 22e3a38
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/Controllers/EnvController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GeoSot\EnvEditor\Controllers;

use GeoSot\EnvEditor\Facades\EnvEditor;
use GeoSot\EnvEditor\EnvEditor;
use GeoSot\EnvEditor\ServiceProvider;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
Expand All @@ -14,15 +14,20 @@

class EnvController extends BaseController
{
public function __construct(
protected EnvEditor $envEditor
) {
}

/**
* Display main view with the Collection of current .env values.
*
* @return JsonResponse|View
*/
public function index()
public function index(Request $request)
{
$envValues = EnvEditor::getEnvFileContent();
if (request()->wantsJson()) {
$envValues = $this->envEditor->getEnvFileContent();
if ($request->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $envValues]);
}

Expand All @@ -34,7 +39,7 @@ public function index()
*/
public function addKey(Request $request): JsonResponse
{
$result = EnvEditor::addKey(
$result = $this->envEditor->addKey(
$request->input('key'),
$request->input('value'),
$request->except(['key', 'value'])
Expand All @@ -48,7 +53,7 @@ public function addKey(Request $request): JsonResponse
*/
public function editKey(Request $request): JsonResponse
{
$result = EnvEditor::editKey($request->input('key'), $request->input('value'));
$result = $this->envEditor->editKey($request->input('key'), $request->input('value'));

return $this->returnGenericResponse($result, [], 'keyWasEdited', $request->input('key'));
}
Expand All @@ -58,20 +63,18 @@ public function editKey(Request $request): JsonResponse
*/
public function deleteKey(Request $request): JsonResponse
{
$result = EnvEditor::deleteKey($request->input('key'));
$result = $this->envEditor->deleteKey($request->input('key'));

return $this->returnGenericResponse($result, [], 'keyWasDeleted', $request->input('key'));
}

/**
* Display a listing of the resource.
*
* @return View|JsonResponse
*/
public function getBackupFiles()
public function getBackupFiles(Request $request): View|JsonResponse
{
$backUpFiles = EnvEditor::getAllBackUps();
if (request()->wantsJson()) {
$backUpFiles = $this->envEditor->getAllBackUps();
if ($request->wantsJson()) {
return $this->returnGenericResponse(true, ['items' => $backUpFiles]);
}

Expand All @@ -83,7 +86,7 @@ public function getBackupFiles()
*/
public function createBackup(): JsonResponse
{
$result = EnvEditor::backUpCurrent();
$result = $this->envEditor->backUpCurrent();

return $this->returnGenericResponse($result, [], 'backupWasCreated');
}
Expand All @@ -93,7 +96,7 @@ public function createBackup(): JsonResponse
*/
public function restoreBackup(string $filename): JsonResponse
{
$result = EnvEditor::restoreBackUp($filename);
$result = $this->envEditor->restoreBackUp($filename);

return $this->returnGenericResponse($result, [], 'fileWasRestored', $filename);
}
Expand All @@ -103,7 +106,7 @@ public function restoreBackup(string $filename): JsonResponse
*/
public function destroyBackup(string $filename): JsonResponse
{
$result = EnvEditor::deleteBackup($filename);
$result = $this->envEditor->deleteBackup($filename);

return $this->returnGenericResponse($result, [], 'fileWasDeleted', $filename);
}
Expand All @@ -113,7 +116,7 @@ public function destroyBackup(string $filename): JsonResponse
*/
public function download(string $filename = ''): BinaryFileResponse
{
$path = EnvEditor::getFilePath($filename);
$path = $this->envEditor->getFilePath($filename);

return response()->download($path);
}
Expand All @@ -130,7 +133,7 @@ public function upload(Request $request): JsonResponse

/** @var UploadedFile $uploadedFile */
$uploadedFile = $request->file('file');
$file = EnvEditor::upload($uploadedFile, $replaceCurrentEnv);
$file = $this->envEditor->upload($uploadedFile, $replaceCurrentEnv);
$successMsg = ($replaceCurrentEnv) ? 'currentEnvWasReplacedByTheUploadedFile' : 'uploadedFileSavedAsBackup';

return $this->returnGenericResponse(true, [], $successMsg, $file->getFilename());
Expand Down

0 comments on commit 22e3a38

Please sign in to comment.