Browse Source

Add generic class for file roles

pull/330/head
James Cole 3 years ago
parent
commit
5303b9f3da
No known key found for this signature in database GPG Key ID: B49A324B7EAD6D80
  1. 75
      .ci/php-cs-fixer/composer.lock
  2. 1
      .ci/phpcs.sh
  3. 2
      app/Console/AutoImports.php
  4. 130
      app/Http/Controllers/Import/File/RoleController.php
  5. 2
      app/Http/Request/RolesPostRequest.php
  6. 0
      resources/views/import/005-roles/index-csv.twig
  7. 6
      routes/web.php

75
.ci/php-cs-fixer/composer.lock

@ -379,16 +379,16 @@
},
{
"name": "friendsofphp/php-cs-fixer",
"version": "v3.15.1",
"version": "v3.16.0",
"source": {
"type": "git",
"url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
"reference": "d48755372a113bddb99f749e34805d83f3acfe04"
"reference": "d40f9436e1c448d309fa995ab9c14c5c7a96f2dc"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/d48755372a113bddb99f749e34805d83f3acfe04",
"reference": "d48755372a113bddb99f749e34805d83f3acfe04",
"url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/d40f9436e1c448d309fa995ab9c14c5c7a96f2dc",
"reference": "d40f9436e1c448d309fa995ab9c14c5c7a96f2dc",
"shasum": ""
},
"require": {
@ -463,7 +463,7 @@
],
"support": {
"issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.15.1"
"source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.16.0"
},
"funding": [
{
@ -471,7 +471,7 @@
"type": "github"
}
],
"time": "2023-03-13T23:26:30+00:00"
"time": "2023-04-02T19:30:06+00:00"
},
{
"name": "psr/cache",
@ -677,16 +677,16 @@
},
{
"name": "sebastian/diff",
"version": "5.0.0",
"version": "5.0.1",
"source": {
"type": "git",
"url": "https://github.com/sebastianbergmann/diff.git",
"reference": "70dd1b20bc198da394ad542e988381b44e64e39f"
"reference": "aae9a0a43bff37bd5d8d0311426c87bf36153f02"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/70dd1b20bc198da394ad542e988381b44e64e39f",
"reference": "70dd1b20bc198da394ad542e988381b44e64e39f",
"url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/aae9a0a43bff37bd5d8d0311426c87bf36153f02",
"reference": "aae9a0a43bff37bd5d8d0311426c87bf36153f02",
"shasum": ""
},
"require": {
@ -731,7 +731,8 @@
],
"support": {
"issues": "https://github.com/sebastianbergmann/diff/issues",
"source": "https://github.com/sebastianbergmann/diff/tree/5.0.0"
"security": "https://github.com/sebastianbergmann/diff/security/policy",
"source": "https://github.com/sebastianbergmann/diff/tree/5.0.1"
},
"funding": [
{
@ -739,20 +740,20 @@
"type": "github"
}
],
"time": "2023-02-03T07:00:31+00:00"
"time": "2023-03-23T05:12:41+00:00"
},
{
"name": "symfony/console",
"version": "v6.2.7",
"version": "v6.2.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/console.git",
"reference": "cbad09eb8925b6ad4fb721c7a179344dc4a19d45"
"reference": "3582d68a64a86ec25240aaa521ec8bc2342b369b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/console/zipball/cbad09eb8925b6ad4fb721c7a179344dc4a19d45",
"reference": "cbad09eb8925b6ad4fb721c7a179344dc4a19d45",
"url": "https://api.github.com/repos/symfony/console/zipball/3582d68a64a86ec25240aaa521ec8bc2342b369b",
"reference": "3582d68a64a86ec25240aaa521ec8bc2342b369b",
"shasum": ""
},
"require": {
@ -814,12 +815,12 @@
"homepage": "https://symfony.com",
"keywords": [
"cli",
"command line",
"command-line",
"console",
"terminal"
],
"support": {
"source": "https://github.com/symfony/console/tree/v6.2.7"
"source": "https://github.com/symfony/console/tree/v6.2.8"
},
"funding": [
{
@ -835,7 +836,7 @@
"type": "tidelift"
}
],
"time": "2023-02-25T17:00:03+00:00"
"time": "2023-03-29T21:42:15+00:00"
},
{
"name": "symfony/deprecation-contracts",
@ -906,16 +907,16 @@
},
{
"name": "symfony/event-dispatcher",
"version": "v6.2.7",
"version": "v6.2.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/event-dispatcher.git",
"reference": "404b307de426c1c488e5afad64403e5f145e82a5"
"reference": "04046f35fd7d72f9646e721fc2ecb8f9c67d3339"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/404b307de426c1c488e5afad64403e5f145e82a5",
"reference": "404b307de426c1c488e5afad64403e5f145e82a5",
"url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/04046f35fd7d72f9646e721fc2ecb8f9c67d3339",
"reference": "04046f35fd7d72f9646e721fc2ecb8f9c67d3339",
"shasum": ""
},
"require": {
@ -969,7 +970,7 @@
"description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/event-dispatcher/tree/v6.2.7"
"source": "https://github.com/symfony/event-dispatcher/tree/v6.2.8"
},
"funding": [
{
@ -985,7 +986,7 @@
"type": "tidelift"
}
],
"time": "2023-02-14T08:44:56+00:00"
"time": "2023-03-20T16:06:02+00:00"
},
{
"name": "symfony/event-dispatcher-contracts",
@ -1754,16 +1755,16 @@
},
{
"name": "symfony/process",
"version": "v6.2.7",
"version": "v6.2.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/process.git",
"reference": "680e8a2ea6b3f87aecc07a6a65a203ae573d1902"
"reference": "75ed64103df4f6615e15a7fe38b8111099f47416"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/process/zipball/680e8a2ea6b3f87aecc07a6a65a203ae573d1902",
"reference": "680e8a2ea6b3f87aecc07a6a65a203ae573d1902",
"url": "https://api.github.com/repos/symfony/process/zipball/75ed64103df4f6615e15a7fe38b8111099f47416",
"reference": "75ed64103df4f6615e15a7fe38b8111099f47416",
"shasum": ""
},
"require": {
@ -1795,7 +1796,7 @@
"description": "Executes commands in sub-processes",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/process/tree/v6.2.7"
"source": "https://github.com/symfony/process/tree/v6.2.8"
},
"funding": [
{
@ -1811,7 +1812,7 @@
"type": "tidelift"
}
],
"time": "2023-02-24T10:42:00+00:00"
"time": "2023-03-09T16:20:02+00:00"
},
{
"name": "symfony/service-contracts",
@ -1962,16 +1963,16 @@
},
{
"name": "symfony/string",
"version": "v6.2.7",
"version": "v6.2.8",
"source": {
"type": "git",
"url": "https://github.com/symfony/string.git",
"reference": "67b8c1eec78296b85dc1c7d9743830160218993d"
"reference": "193e83bbd6617d6b2151c37fff10fa7168ebddef"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/string/zipball/67b8c1eec78296b85dc1c7d9743830160218993d",
"reference": "67b8c1eec78296b85dc1c7d9743830160218993d",
"url": "https://api.github.com/repos/symfony/string/zipball/193e83bbd6617d6b2151c37fff10fa7168ebddef",
"reference": "193e83bbd6617d6b2151c37fff10fa7168ebddef",
"shasum": ""
},
"require": {
@ -2028,7 +2029,7 @@
"utf8"
],
"support": {
"source": "https://github.com/symfony/string/tree/v6.2.7"
"source": "https://github.com/symfony/string/tree/v6.2.8"
},
"funding": [
{
@ -2044,7 +2045,7 @@
"type": "tidelift"
}
],
"time": "2023-02-24T10:42:00+00:00"
"time": "2023-03-20T16:06:02+00:00"
}
],
"packages-dev": [],

1
.ci/phpcs.sh

@ -30,6 +30,7 @@ SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# clean up php code
cd $SCRIPT_DIR/php-cs-fixer
rm php-cs-fixer/.php-cs-fixer.cache
composer update
PHP_CS_FIXER_IGNORE_ENV=true ./vendor/bin/php-cs-fixer fix --config $SCRIPT_DIR/php-cs-fixer/.php-cs-fixer.php --allow-risky=yes
cd $SCRIPT_DIR/..

2
app/Console/AutoImports.php

@ -248,7 +248,7 @@ trait AutoImports
$contentType = $configuration->getContentType();
if('unknown' === $contentType) {
app('log')->debug('Content type is "unknown" in startConversion(), detect it.');
$detector = new FileContentSherlock;
$detector = new FileContentSherlock();
$contentType = $detector->detectContentType($importableFile);
}
switch($contentType) {

130
app/Http/Controllers/Import/CSV/RoleController.php → app/Http/Controllers/Import/File/RoleController.php

@ -22,13 +22,15 @@
declare(strict_types=1);
namespace App\Http\Controllers\Import\CSV;
namespace App\Http\Controllers\Import\File;
use App\Exceptions\ImporterErrorException;
use App\Http\Controllers\Controller;
use App\Http\Middleware\RoleControllerMiddleware;
use App\Http\Request\RolesPostRequest;
use App\Services\CSV\Roles\RoleService;
use App\Services\Session\Constants;
use App\Services\Shared\Configuration\Configuration;
use App\Services\Storage\StorageService;
use App\Support\Http\RestoresConfiguration;
use Illuminate\Contracts\View\Factory;
@ -69,42 +71,28 @@ class RoleController extends Controller
* @throws UnableToProcessCsv
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ImporterErrorException
*/
public function index(Request $request)
{
app('log')->debug('Now in role controller');
app('log')->debug('Now in Role controller');
$flow = $request->cookie(Constants::FLOW_COOKIE);
if ('file' !== $flow) {
die('redirect or something');
}
$mainTitle = 'Role definition';
$subTitle = 'Configure the role of each column in your file';
// get configuration object.
// Read configuration from session, will miss some important keys:
$configuration = $this->restoreConfiguration();
$contentType = $configuration->getContentType();
switch ($contentType) {
default:
throw new ImporterErrorException(sprintf('Cannot handle file type "%s"', $contentType));
case 'csv':
return $this->csvIndex($request, $configuration);
case 'camt':
return $this->camtIndex($request, $configuration);
// get columns from file
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion());
$columns = RoleService::getColumns($content, $configuration);
$examples = RoleService::getExampleData($content, $configuration);
// submit mapping from config.
$mapping = base64_encode(json_encode($configuration->getMapping(), JSON_THROW_ON_ERROR));
// roles
$roles = config('csv.import_roles');
ksort($roles);
// configuration (if it is set)
$configuredRoles = $configuration->getRoles();
$configuredDoMapping = $configuration->getDoMapping();
return view(
'import.005-roles.index',
compact('mainTitle', 'configuration', 'subTitle', 'columns', 'examples', 'roles', 'configuredRoles', 'configuredDoMapping', 'mapping')
);
}
}
/**
@ -117,12 +105,33 @@ class RoleController extends Controller
*/
public function postIndex(RolesPostRequest $request): RedirectResponse
{
$data = $request->getAll();
// get configuration object.
// Read configuration from session, may miss some important keys:
// the request object must be able to handle all file types.
$configuration = $this->restoreConfiguration();
$contentType = $configuration->getContentType();
switch ($contentType) {
default:
throw new ImporterErrorException(sprintf('Cannot handle file type "%s" in POST.', $contentType));
case 'csv':
return $this->csvPostIndex($request, $configuration);
case 'camt':
return $this->camtPostIndex($request, $configuration);
}
}
/**
* @param RolesPostRequest $request
* @param Configuration $configuration
*
* @return RedirectResponse
* @throws ContainerExceptionInterface
* @throws JsonException
* @throws NotFoundExceptionInterface
*/
private function csvPostIndex(RolesPostRequest $request, Configuration $configuration): RedirectResponse
{
$data = $request->getAllForCSV();
$needsMapping = $this->needMapping($data['do_mapping']);
$configuration->setRoles($data['roles']);
$configuration->setDoMapping($data['do_mapping']);
@ -173,4 +182,65 @@ class RoleController extends Controller
return $need;
}
/**
* @param Request $request
* @param Configuration $configuration
*
* @return View
* @throws ContainerExceptionInterface
* @throws Exception
* @throws InvalidArgument
* @throws JsonException
* @throws NotFoundExceptionInterface
* @throws UnableToProcessCsv
*/
private function csvIndex(Request $request, Configuration $configuration): View
{
$mainTitle = 'Role definition';
$subTitle = 'Configure the role of each column in your file';
// get columns from file
$content = StorageService::getContent(session()->get(Constants::UPLOAD_CSV_FILE), $configuration->isConversion());
$columns = RoleService::getColumns($content, $configuration);
$examples = RoleService::getExampleData($content, $configuration);
// submit mapping from config.
$mapping = base64_encode(json_encode($configuration->getMapping(), JSON_THROW_ON_ERROR));
// roles
$roles = config('csv.import_roles');
ksort($roles);
// configuration (if it is set)
$configuredRoles = $configuration->getRoles();
$configuredDoMapping = $configuration->getDoMapping();
return view(
'import.005-roles.index-csv',
compact('mainTitle', 'configuration', 'subTitle', 'columns', 'examples', 'roles', 'configuredRoles', 'configuredDoMapping', 'mapping')
);
}
/**
* @param Request $request
* @param Configuration $configuration
*
* @return View
*/
private function camtIndex(Request $request, Configuration $configuration): View
{
return view('import.0005-roles.index-camt');
}
/**
* @param RolesPostRequest $request
* @param Configuration $configuration
*
* @return void
*/
private function camtPostIndex(RolesPostRequest $request, Configuration $configuration)
{
die('not yet implemented.');
}
}

2
app/Http/Request/RolesPostRequest.php

@ -44,7 +44,7 @@ class RolesPostRequest extends Request
/**
* @return array
*/
public function getAll(): array
public function getAllForCSV(): array
{
$data = [
'roles' => $this->get('roles') ?? [],

0
resources/views/import/005-roles/index.twig → resources/views/import/005-roles/index-csv.twig

6
routes/web.php

@ -59,10 +59,10 @@ Route::post('/import/configure', ['uses' => 'Import\ConfigurationController@post
Route::get('/import/configure/download', ['uses' => 'Import\DownloadController@download', 'as' => '004-configure.download']);
Route::get('/import/php_date', ['uses' => 'Import\ConfigurationController@phpDate', 'as' => '004-configure.php_date']);
// step 5: Set column roles (CSV)
// step 5: Set column roles (CSV or other file types)
// check : must be CSV and not config complete otherwise redirect to mapping.
Route::get('/import/roles', ['uses' => 'Import\CSV\RoleController@index', 'as' => '005-roles.index']);
Route::post('/import/roles', ['uses' => 'Import\CSV\RoleController@postIndex', 'as' => '005-roles.post']);
Route::get('/import/roles', ['uses' => 'Import\File\RoleController@index', 'as' => '005-roles.index']);
Route::post('/import/roles', ['uses' => 'Import\File\RoleController@postIndex', 'as' => '005-roles.post']);
// step 6: mapping
// check: must be [CSV and roles complete] or [not csv and conversion complete] or go to conversion?

Loading…
Cancel
Save