Some issues fixed for scrutiniser.

This commit is contained in:
James Cole 2016-09-25 08:20:17 +02:00
parent 9101d6a2c0
commit bb7c26b77c
4 changed files with 47 additions and 14 deletions

View File

@ -59,7 +59,7 @@ class AttachmentHelper implements AttachmentHelperInterface
*/ */
public function getAttachmentLocation(Attachment $attachment): string public function getAttachmentLocation(Attachment $attachment): string
{ {
$path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, $attachment->id); $path = sprintf('%s%sat-%d.data', storage_path('upload'), DIRECTORY_SEPARATOR, intval($attachment->id));
return $path; return $path;
} }

View File

@ -416,17 +416,24 @@ class ImportController extends Controller
* @param ImportJob $job * @param ImportJob $job
* *
* @return SetupInterface * @return SetupInterface
* @throws FireflyException
*/ */
private function makeImporter(ImportJob $job): SetupInterface private function makeImporter(ImportJob $job): SetupInterface
{ {
// create proper importer (depends on job) // create proper importer (depends on job)
$type = $job->file_type; $type = strtolower($job->file_type);
/** @var SetupInterface $importer */ // validate type:
$importer = app('FireflyIII\Import\Setup\\' . ucfirst($type) . 'Setup'); $validTypes = array_keys('firefly.import_formats');
$importer->setJob($job);
return $importer; if (in_array($type, $validTypes)) {
/** @var SetupInterface $importer */
$importer = app('FireflyIII\Import\Setup\\' . ucfirst($type) . 'Setup');
$importer->setJob($job);
return $importer;
}
throw new FireflyException(sprintf('"%s" is not a valid file type', $type));
} }

View File

@ -11,6 +11,7 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Importer; namespace FireflyIII\Import\Importer;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Converter\ConverterInterface; use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\ImportEntry; use FireflyIII\Import\ImportEntry;
use FireflyIII\Import\Specifics\SpecificInterface; use FireflyIII\Import\Specifics\SpecificInterface;
@ -120,8 +121,14 @@ class CsvImporter implements ImporterInterface
foreach ($row as $rowIndex => $value) { foreach ($row as $rowIndex => $value) {
// find the role for this column: // find the role for this column:
$role = $config['column-roles'][$rowIndex] ?? '_ignore'; $role = $config['column-roles'][$rowIndex] ?? '_ignore';
$doMap = $config['column-do-mapping'][$rowIndex] ?? false; $doMap = $config['column-do-mapping'][$rowIndex] ?? false;
$validConverters = array_keys('csv.import_roles');
// throw error when not a valid converter.
if (!in_array($role, $validConverters)) {
throw new FireflyException(sprintf('"%s" is not a valid role.', $role));
}
$converterClass = config('csv.import_roles.' . $role . '.converter'); $converterClass = config('csv.import_roles.' . $role . '.converter');
$mapping = $config['column-mapping-config'][$rowIndex] ?? []; $mapping = $config['column-mapping-config'][$rowIndex] ?? [];
$className = sprintf('FireflyIII\\Import\\Converter\\%s', $converterClass); $className = sprintf('FireflyIII\\Import\\Converter\\%s', $converterClass);

View File

@ -14,6 +14,7 @@ namespace FireflyIII\Import\Setup;
use ExpandedForm; use ExpandedForm;
use FireflyIII\Crud\Account\AccountCrud; use FireflyIII\Crud\Account\AccountCrud;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Import\Mapper\MapperInterface; use FireflyIII\Import\Mapper\MapperInterface;
use FireflyIII\Import\MapperPreProcess\PreProcessorInterface; use FireflyIII\Import\MapperPreProcess\PreProcessorInterface;
use FireflyIII\Import\Specifics\SpecificInterface; use FireflyIII\Import\Specifics\SpecificInterface;
@ -326,6 +327,7 @@ class CsvSetup implements SetupInterface
/** /**
* @return array * @return array
* @throws FireflyException
*/ */
private function getDataForColumnMapping(): array private function getDataForColumnMapping(): array
{ {
@ -335,11 +337,19 @@ class CsvSetup implements SetupInterface
foreach ($config['column-do-mapping'] as $index => $mustBeMapped) { foreach ($config['column-do-mapping'] as $index => $mustBeMapped) {
if ($mustBeMapped) { if ($mustBeMapped) {
$column = $config['column-roles'][$index] ?? '_ignore'; $column = $config['column-roles'][$index] ?? '_ignore';
// is valid column?
$validColumns = array_keys(config('csv.import_roles'));
if (!in_array($column, $validColumns)) {
throw new FireflyException(sprintf('"%s" is not a valid column.', $column));
}
$canBeMapped = config('csv.import_roles.' . $column . '.mappable'); $canBeMapped = config('csv.import_roles.' . $column . '.mappable');
$preProcessMap = config('csv.import_roles.' . $column . '.pre-process-map'); $preProcessMap = config('csv.import_roles.' . $column . '.pre-process-map');
if ($canBeMapped) { if ($canBeMapped) {
$mapperName = '\FireflyIII\Import\Mapper\\' . config('csv.import_roles.' . $column . '.mapper'); $mapperClass = config('csv.import_roles.' . $column . '.mapper');
$mapperName = sprintf('\\FireflyIII\\Import\Mapper\\%s', $mapperClass);
/** @var MapperInterface $mapper */ /** @var MapperInterface $mapper */
$mapper = new $mapperName; $mapper = new $mapperName;
$indexes[] = $index; $indexes[] = $index;
@ -352,8 +362,11 @@ class CsvSetup implements SetupInterface
'values' => [], 'values' => [],
]; ];
if ($preProcessMap) { if ($preProcessMap) {
$data[$index]['preProcessMap'] = '\FireflyIII\Import\MapperPreProcess\\' . $preClass = sprintf(
config('csv.import_roles.' . $column . '.pre-process-mapper'); '\\FireflyIII\\Import\\MapperPreProcess\\%s',
config('csv.import_roles.' . $column . '.pre-process-mapper')
);
$data[$index]['preProcessMap'] = $preClass;
} }
} }
@ -365,15 +378,21 @@ class CsvSetup implements SetupInterface
/** @var Reader $reader */ /** @var Reader $reader */
$reader = Reader::createFromString($content); $reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']); $reader->setDelimiter($config['delimiter']);
$results = $reader->fetch(); $results = $reader->fetch();
$validSpecifics = array_keys('csv.import_specifics');
foreach ($results as $rowIndex => $row) { foreach ($results as $rowIndex => $row) {
// run specifics here: // run specifics here:
// and this is the point where the specifix go to work. // and this is the point where the specifix go to work.
foreach ($config['specifics'] as $name => $enabled) { foreach ($config['specifics'] as $name => $enabled) {
if (!in_array($name, $validSpecifics)) {
throw new FireflyException(sprintf('"%s" is not a valid class name', $name));
}
$class = config('csv.import_specifics.' . $name);
/** @var SpecificInterface $specific */ /** @var SpecificInterface $specific */
$specific = app('FireflyIII\Import\Specifics\\' . $name); $specific = app($class);
// it returns the row, possibly modified: // it returns the row, possibly modified:
$row = $specific->run($row); $row = $specific->run($row);