Browse Source

Correct middleware routine.

pull/1/head
James Cole 4 years ago
parent
commit
b0d3f58ea5
No known key found for this signature in database GPG Key ID: BDE6667570EADBD5
  1. 3
      app/Http/Controllers/Import/UploadController.php
  2. 3
      app/Http/Controllers/IndexController.php
  3. 123
      app/Http/Middleware/IsReadyForStep.php
  4. 64
      app/Http/Middleware/UploadControllerMiddleware.php

3
app/Http/Controllers/Import/UploadController.php

@ -27,6 +27,7 @@ namespace App\Http\Controllers\Import;
use App\Exceptions\ImporterErrorException;
use App\Http\Controllers\Controller;
use App\Http\Middleware\UploadControllerMiddleware;
use App\Http\Middleware\UploadedFiles;
use App\Services\CSV\Configuration\ConfigFileProcessor;
use App\Services\Session\Constants;
@ -52,7 +53,7 @@ class UploadController extends Controller
{
parent::__construct();
app('view')->share('pageTitle', 'Upload files');
$this->middleware(UploadedFiles::class);
$this->middleware(UploadControllerMiddleware::class);
}
/**

3
app/Http/Controllers/IndexController.php

@ -46,8 +46,9 @@ class IndexController extends Controller
/**
* @param Request $request
* @return mixed
*/
public function postIndex(Request $request)
public function postIndex(Request $request): mixed
{
// set cookie with flow:
$flow = $request->get('flow');

123
app/Http/Middleware/IsReadyForStep.php

@ -0,0 +1,123 @@
<?php
/*
* IsReadyForStep.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of the Firefly III Data Importer
* (https://github.com/firefly-iii/data-importer).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Http\Middleware;
use App\Exceptions\ImporterErrorException;
use App\Services\Session\Constants;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Log;
/**
* Trait IsReadyForStep
*/
trait IsReadyForStep
{
/**
* @param Request $request
* @param string $step
* @return bool
* @throws ImporterErrorException
*/
protected function isReadyForStep(Request $request, string $step): bool
{
$flow = $request->cookie('flow');
if (null === $flow) {
Log::debug('isReadyForStep returns true because $flow is null');
return true;
}
if ('csv' === $flow) {
return $this->isReadyForCSVStep($request, $step);
}
if ('nordigen' === $flow) {
die('TODO');
}
if ('spectre' === $flow) {
die('TODO');
}
}
/**
* @param Request $request
* @param string $step
* @return bool
* @throws ImporterErrorException
*/
private function isReadyForCSVStep(Request $request, string $step): bool
{
Log::debug(sprintf('isReadyForCSVStep("%s")', $step));
switch ($step) {
default:
throw new ImporterErrorException(sprintf('isReadyForCSVStep: Cannot handle CSV step "%s"', $step));
case 'upload-files':
if (session()->has(Constants::HAS_UPLOAD) && true === session()->get(Constants::HAS_UPLOAD)) {
return false;
}
return true;
}
}
/**
* @param Request $request
* @param string $step
* @return RedirectResponse|null
* @throws ImporterErrorException
*/
protected function redirectToCorrectStep(Request $request, string $step): ?RedirectResponse
{
$flow = $request->cookie('flow');
if (null === $flow) {
Log::debug('redirectToCorrectStep returns true because $flow is null');
return null;
}
if ('csv' === $flow) {
return $this->redirectToCorrectCSVStep($step);
}
if ('nordigen' === $flow) {
die('TODO');
}
if ('spectre' === $flow) {
die('TODO');
}
die('TODO');
}
/**
* @param $step
* @return RedirectResponse
* @throws ImporterErrorException
*/
private function redirectToCorrectCSVStep($step): RedirectResponse
{
Log::debug(sprintf('redirectToCorrectCSVStep("%s")', $step));
switch ($step) {
default:
throw new ImporterErrorException(sprintf('redirectToCorrectCSVStep: Cannot handle CSV step "%s"', $step));
case 'upload-files':
$route = route('004-configure.index');
Log::debug(sprintf('Return redirect to "%s"', $route));
return redirect($route);
}
}
}

64
app/Http/Middleware/UploadControllerMiddleware.php

@ -0,0 +1,64 @@
<?php
/*
* UploadControllerMiddleware.php
* Copyright (c) 2021 james@firefly-iii.org
*
* This file is part of the Firefly III Data Importer
* (https://github.com/firefly-iii/data-importer).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Http\Middleware;
use App\Exceptions\ImporterErrorException;
use App\Services\Session\Constants;
use Illuminate\Http\Request;
use Closure;
/**
* Class UploadControllerMiddleware
*/
class UploadControllerMiddleware
{
use IsReadyForStep;
/**
* Check if the user has already uploaded files in this session. If so, continue to configuration.
*
* @param Request $request
* @param Closure $next
*
* @return mixed
*
*/
public function handle(Request $request, Closure $next)
{
$step = 'upload-files';
$result = $this->isReadyForStep($request, $step);
if(true === $result) {
return $next($request);
}
$redirect = $this->redirectToCorrectStep($request, $step);
if(null !== $redirect) {
return $redirect;
}
throw new ImporterErrorException('Cannot handle upload controller');
}
}
Loading…
Cancel
Save