有时我有非常大的函数,我觉得很难“压缩”或分离成更小的函数,因为这些函数不会在脚本的任何其他部分中使用。
所以,我想要一些建议:我应该创建不会在脚本的其他部分使用的函数,还是应该只在它们被多次使用时才创建它们?
谢谢!
概括:
- 用目录中文件的信息填充数组。
- 逐行处理 TXT,查看 TXT 中的 ID 是否与“已完成”文件数组匹配,并将其发布到外部产品中。如果没有,则检查其他数组以报告缺少的内容。
- 保存在数组中找到的错误,然后将数组保存到 errors.txt
- 文件。最后,返回报告。
目前我的功能是:
protected function processScanned()
{
try
{
// EJECUTAR BASH DE NAHUEL
//
//
$PdfCPList = $this->model->getDirFilenames( $this->model->dirCartasPorte, 'pdf' );
$PdfTBList = $this->model->getDirFilenames( $this->model->dirTicketsBalanza, 'pdf' );
$PdfCompList = $this->model->getDirFilenames( $this->model->dirCompletos, 'pdf' );
$PdfUnreconList = $this->model->getDirFilenames( $this->model->dirSinReconocer,'pdf' );
// Adjuntar Novedades
$newsToProcess = $this->model->getDirFilenames( $this->model->dirNovedades, 'txt', true);
$this->appendNewsFiles($newsToProcess);
$report = array();
$report['info'] = array(
'Documentos procesados correctamente'=>0,
'Fecha de última actualización de datos'=>date('d/m/Y H:i:s',(int)file_get_contents($this->model->uriTxtInfo)),
);
if($file = fopen( $this->model->uriTxtProcesar, 'r' ) )
{
$i = 0;
$errors_file = fopen($this->model->uriTxtErrores,'w');
while( $line = fgets( $file ) )
{
if( ! preg_match( '/^\s/', $line ) )
continue;
$lineData = array(
'id'=> substr($line,3,9),
'prefix'=>'1234-' . $i,
'suffix'=>'1234-' . $i,
'partner'=>'FAZON TIMBUES OMHSA',
'date'=>time() - 222,
);
$i++;
$keywordsToPublish = array(
'Nº de Operacion'=>$lineData['id'],
'Prefijo'=>$lineData['prefix'],
'Sufijo'=>$lineData['suffix'],
'Socio'=>$lineData['partner'],
'Fecha'=>date('Y/d/m',$lineData['date']),
);
if( $this->model->findInDocusearch( $lineData['id'] ) )
{
continue;
}
if( array_key_exists( $lineData['id'], $PdfCompList ) )
{
$lineData['docName'] = 'Carta de Porte - Ticket de Balanza';
$lineData['docId'] = 'CP-TB';
$lineData['path'] = $this->model->dirCompletos . '/' . $lineData['id'] . '.pdf';
if( $id = $this->model->publishInDocusearch( $lineData, $keywordsToPublish ) ) {
$report['info']['Documentos procesados correctamente']++;
link( $this->model->dirDocusearchRepo . '/' . $id . '.pdf',
$this->model->dirBackupCliente . '/' . $lineData['partner'] . '_' . date('Ymd',$lineData['date']) . '_' . $lineData['id'] . '.pdf'
);
}
unset( $PdfCompList[ $lineData['id'] ] );
}
else
{
fwrite($errors_file, $line); // Guarda la fila leida en el archivo de errores.
// Valores por defecto
$report[ 'errors' ][ $lineData['id'] ]['date'] = $lineData['date'];
$report[ 'errors' ][ $lineData['id'] ]['id'] = $lineData['id'];
$report[ 'errors' ][ $lineData['id'] ]['type'] = 'nn';
$report[ 'errors' ][ $lineData['id'] ]['actions'] = array();
// Valores por defecto
if( array_key_exists( $lineData['id'], $PdfCPList ) )
{
$report[ 'errors' ][ $lineData['id'] ]['reportMsg'] = 'Falta Ticket de Balanza.';
$report[ 'errors' ][ $lineData['id'] ]['type'] = 'cp';
unset( $PdfCPList[ $lineData['id'] ] );
}
elseif( array_key_exists( $lineData['id'], $PdfTBList ) )
{
$report[ 'errors' ][ $lineData['id'] ]['reportMsg'] = 'Falta Carta de Porte.';
$report[ 'errors' ][ $lineData['id'] ]['type'] = 'tb';
unset( $PdfTBList[ $lineData['id'] ] );
}
else
{
$report[ 'errors' ][ $lineData['id'] ]['reportMsg'] = 'Ningún archivo digitalizado.';
}
}
}
fclose( $file );
fclose( $errors_file );
$this->fileRename( $this->model->uriTxtErrores, $this->model->uriTxtProcesar);
foreach( $PdfCompList as $key=>$value )
{
$report[ 'errors' ][ $key ] = array(
'reportMsg'=>'Falta en archivo de datos del sistema externo.',
'date'=>$value['date'],
'id'=>$key,
'type'=>'co',
'actions'=>array('get','rename','delete'),
);
}
foreach( $PdfCPList as $key=>$value )
{
$report[ 'errors' ][ $key ] = array(
'reportMsg'=>'Carta de Porte no utilizada.',
'date'=>$value['date'],
'id'=>$key,
'type'=>'cp',
'actions'=>array('get','rename','delete'),
);
}
foreach( $PdfTBList as $key=>$value )
{
$report[ 'errors' ][ $key ] = array(
'reportMsg'=>'Ticket de Balanza no utilizado.',
'date'=>$value['date'],
'id'=>$key,
'type'=>'tb',
'actions'=>array('get','rename','delete'),
);
}
foreach( $PdfUnreconList as $key=>$value )
{
$report[ 'errors' ][ $key ] = array(
'reportMsg'=>'Documento no reconocido.',
'date'=>$value['date'],
'id'=>$key,
'type'=>'un',
'actions'=>array('get','rename','delete'),
);
}
return $report;
}
else
{
throw new Exception('No se pudo abrir el archivo TXT');
}
}
catch( Exception $e )
{
$this->mensaje = $e->getMessage();
header('HTTP/1.1 500 ' . $this->mensaje);
}
}