Browse Source

Refactor the map controller a bit.

pull/17/head
James Cole 4 years ago
parent
commit
56c72e9278
No known key found for this signature in database GPG Key ID: BDE6667570EADBD5
  1. 3
      app/Http/Controllers/Import/CSV/RoleController.php
  2. 4
      app/Http/Controllers/Import/ConfigurationController.php
  3. 5
      app/Http/Controllers/Import/DownloadController.php
  4. 141
      app/Http/Controllers/Import/MapController.php
  5. 11
      app/Http/Controllers/Import/Nordigen/SelectionController.php

3
app/Http/Controllers/Import/CSV/RoleController.php

@ -28,8 +28,6 @@ namespace App\Http\Controllers\Import\CSV;
use App\Http\Controllers\Controller;
use App\Http\Middleware\RoleControllerMiddleware;
use App\Http\Request\RolesPostRequest;
use App\Services\CSV\Configuration\ConfigFileProcessor;
use App\Services\CSV\Configuration\Configuration;
use App\Services\CSV\Roles\RoleService;
use App\Services\Session\Constants;
use App\Services\Storage\StorageService;
@ -46,6 +44,7 @@ use Log;
class RoleController extends Controller
{
use RestoresConfiguration;
/**
* RoleController constructor.
*/

4
app/Http/Controllers/Import/ConfigurationController.php

@ -38,7 +38,6 @@ use App\Services\Nordigen\Response\ListAccountsResponse;
use App\Services\Nordigen\Services\AccountInformationCollector;
use App\Services\Nordigen\TokenManager;
use App\Services\Session\Constants;
use App\Services\Spectre\Model\Account as SpectreAccount;
use App\Services\Spectre\Request\GetAccountsRequest as SpectreGetAccountsRequest;
use App\Services\Spectre\Response\GetAccountsResponse as SpectreGetAccountsResponse;
use App\Services\Storage\StorageService;
@ -63,6 +62,7 @@ use Log;
class ConfigurationController extends Controller
{
use RestoresConfiguration;
/**
* StartController constructor.
*/
@ -86,7 +86,7 @@ class ConfigurationController extends Controller
$flow = $request->cookie(Constants::FLOW_COOKIE);
// create configuration:
$configuration = $this->restoreConfiguration();
$configuration = $this->restoreConfiguration();
// if config says to skip it, skip it:
$overruleSkip = 'true' === $request->get('overruleskip');

5
app/Http/Controllers/Import/DownloadController.php

@ -27,6 +27,7 @@ namespace App\Http\Controllers\Import;
use App\Http\Controllers\Controller;
use App\Support\Http\RestoresConfiguration;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
@ -38,9 +39,9 @@ class DownloadController extends Controller
use RestoresConfiguration;
/**
* @return \Illuminate\Contracts\Foundation\Application|ResponseFactory|Response
* @return Application|ResponseFactory|Response
*/
public function download(): Response|\Illuminate\Contracts\Foundation\Application|ResponseFactory
public function download(): Response|Application|ResponseFactory
{
// do something
$configuration = $this->restoreConfiguration();

141
app/Http/Controllers/Import/MapController.php

@ -72,66 +72,101 @@ class MapController extends Controller
*/
public function index()
{
$mainTitle = 'Map data';
$subTitle = 'Map values in file to actual data in Firefly III';
$data = [];
$roles = [];
Log::debug('Now in mapController index');
// get configuration object.
$mainTitle = 'Map data';
$subTitle = 'Map values in file to actual data in Firefly III';
$configuration = $this->restoreConfiguration();
$data = [];
$roles = [];
// depends on flow how to handle mapping
if ('csv' === $configuration->getFlow()) {
$roles = $configuration->getRoles();
$data = $this->getCSVMapInformation();
}
// then we can use them:
$roles = $configuration->getRoles();
$existingMapping = $configuration->getMapping();
$doMapping = $configuration->getDoMapping();
$data = [];
foreach ($roles as $index => $role) {
$info = config('csv.import_roles')[$role] ?? null;
$mappable = $info['mappable'] ?? false;
if (null === $info) {
continue;
}
if (false === $mappable) {
continue;
}
$mapColumn = $doMapping[$index] ?? false;
if (false === $mapColumn) {
continue;
}
Log::debug(sprintf('Mappable role is "%s"', $role));
// nordigen, spectre and others:
if ('csv' !== $configuration->getFlow()) {
$roles = [];
$data = $this->getImporterMapInformation();
}
$info['role'] = $role;
$info['values'] = [];
// if nothing to map, just set mappable to true and go to the next step:
if (0 === count($data)) {
// set map config as complete.
session()->put(Constants::MAPPING_COMPLETE_INDICATOR, true);
return redirect()->route('007-convert.index');
}
// create the "mapper" class which will get data from Firefly III.
$class = sprintf('App\\Services\\CSV\\Mapper\\%s', $info['mapper']);
if (!class_exists($class)) {
throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class));
}
Log::debug(sprintf('Associated class is %s', $class));
return view('import.006-mapping.index', compact('mainTitle', 'subTitle', 'roles', 'data'));
}
/**
* Return the map data necessary for the CSV mapping based on some weird helpers.
* TODO needs refactoring and proper splitting into helpers.
*
* @return array
*/
private function getCSVMapInformation(): array
{
$configuration = $this->restoreConfiguration();
$roles = $configuration->getRoles();
$existingMapping = $configuration->getMapping();
$doMapping = $configuration->getDoMapping();
$data = [];
foreach ($roles as $index => $role) {
$info = config('csv.import_roles')[$role] ?? null;
$mappable = $info['mappable'] ?? false;
if (null === $info) {
continue;
}
if (false === $mappable) {
continue;
}
$mapColumn = $doMapping[$index] ?? false;
if (false === $mapColumn) {
continue;
}
Log::debug(sprintf('Mappable role is "%s"', $role));
/** @var MapperInterface $object */
$object = app($class);
$info['mapping_data'] = $object->getMap();
$info['mapped'] = $existingMapping[$index] ?? [];
$info['role'] = $role;
$info['values'] = [];
Log::debug(sprintf('Mapping data length is %d', count($info['mapping_data'])));
$data[$index] = $info;
// create the "mapper" class which will get data from Firefly III.
$class = sprintf('App\\Services\\CSV\\Mapper\\%s', $info['mapper']);
if (!class_exists($class)) {
throw new InvalidArgumentException(sprintf('Class %s does not exist.', $class));
}
Log::debug(sprintf('Associated class is %s', $class));
// get columns from file
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion());
$delimiter = (string) config(sprintf('csv.delimiters.%s', $configuration->getDelimiter()));
$data = MapperService::getMapData($content, $delimiter, $configuration->isHeaders(), $configuration->getSpecifics(), $data);
/** @var MapperInterface $object */
$object = app($class);
$info['mapping_data'] = $object->getMap();
$info['mapped'] = $existingMapping[$index] ?? [];
Log::debug(sprintf('Mapping data length is %d', count($info['mapping_data'])));
$data[$index] = $info;
}
// get columns from file
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion());
$delimiter = (string) config(sprintf('csv.delimiters.%s', $configuration->getDelimiter()));
return MapperService::getMapData($content, $delimiter, $configuration->isHeaders(), $configuration->getSpecifics(), $data);
}
/**
* Weird bunch of code to return info on Spectre and Nordigen.
* @return array
*/
private function getImporterMapInformation(): array
{
$data = [];
$configuration = $this->restoreConfiguration();
/*
* To map Nordigen, pretend the file has one "column" (this is based on the CSV importer after all)
* that contains:
@ -181,16 +216,7 @@ class MapController extends Controller
$opposingName['mapped'] = $existingMapping[$index] ?? [];
$data[] = $category;
}
// if nothing to map, just set mappable to true and go to the next step:
if (0 === count($data)) {
// set map config as complete.
session()->put(Constants::MAPPING_COMPLETE_INDICATOR, true);
return redirect()->route('007-convert.index');
}
return view('import.006-mapping.index', compact('mainTitle', 'subTitle', 'roles', 'data'));
return $data;
}
/**
@ -235,6 +261,13 @@ class MapController extends Controller
return array_unique($filtered);
}
/**
* @return array
* @throws ContainerExceptionInterface
* @throws FileNotFoundException
* @throws ImporterErrorException
* @throws NotFoundExceptionInterface
*/
private function getCategories(): array
{
Log::debug(sprintf('Now in %s', __METHOD__));

11
app/Http/Controllers/Import/Nordigen/SelectionController.php

@ -36,10 +36,15 @@ use App\Services\Nordigen\TokenManager;
use App\Services\Session\Constants;
use App\Services\Storage\StorageService;
use App\Support\Http\RestoresConfiguration;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
use JsonException;
use Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class SelectionController
@ -90,10 +95,10 @@ class SelectionController extends Controller
/**
* @param SelectionRequest $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @return Application|RedirectResponse|Redirector
* @throws ImporterErrorException
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function postIndex(SelectionRequest $request)
{

Loading…
Cancel
Save