Separate Banco::process pipe to be able to intervene in the middle
This commit is contained in:
@ -9,20 +9,9 @@ abstract class Banco extends Service implements Define\Cartola\Banco
|
||||
{
|
||||
public function process(UploadedFileInterface $file): array
|
||||
{
|
||||
$data = $this->handleFile($file);
|
||||
$temp = [];
|
||||
$columns = $this->columnMap();
|
||||
foreach ($data as $row) {
|
||||
$r = [];
|
||||
foreach ($columns as $old => $new) {
|
||||
if (!isset($row[$old])) {
|
||||
continue;
|
||||
}
|
||||
$r[$new] = $row[$old];
|
||||
}
|
||||
$temp []= $r;
|
||||
}
|
||||
return $temp;
|
||||
$filename = $this->processUploadedFile($file);
|
||||
$data = $this->processFile($filename);
|
||||
return $this->mapColumns($data);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,19 +25,52 @@ abstract class Banco extends Service implements Define\Cartola\Banco
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the UploadedFile into a temp file from getFilename and after parseFile remove temp file
|
||||
* Move the UploadedFile into a temp file from getFilename
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
* @return array
|
||||
* @return string
|
||||
*/
|
||||
protected function handleFile(UploadedFileInterface $uploadedFile): array
|
||||
protected function processUploadedFile(UploadedFileInterface $uploadedFile): string
|
||||
{
|
||||
$filename = $this->getFilename($uploadedFile);
|
||||
$uploadedFile->moveTo($filename);
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the temp file from getFilename and remove it
|
||||
* @param string $filename
|
||||
* @return array
|
||||
*/
|
||||
protected function processFile(string $filename): array
|
||||
{
|
||||
$data = $this->parseFile($filename);
|
||||
unlink($filename);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map columns from uploaded file data to database columns
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
protected function mapColumns(array $data): array
|
||||
{
|
||||
$temp = [];
|
||||
$columns = $this->columnMap();
|
||||
|
||||
foreach ($data as $row) {
|
||||
$r = [];
|
||||
foreach ($columns as $old => $new) {
|
||||
if (!isset($row[$old])) {
|
||||
continue;
|
||||
}
|
||||
$r[$new] = $row[$old];
|
||||
}
|
||||
$temp []= $r;
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filename where to move UploadedFile
|
||||
* @param UploadedFileInterface $uploadedFile
|
||||
|
Reference in New Issue
Block a user