mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
Desacople e inyeccion de dependencias
This commit is contained in:
76
ci4/app/Services/PresupuestoUploaderService.php
Normal file
76
ci4/app/Services/PresupuestoUploaderService.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Presupuestos\PresupuestoFicheroModel;
|
||||
use App\Models\Pedidos\PedidoLineaModel;
|
||||
use App\Libraries\SftpClientWrapper;
|
||||
use Config\PresupuestoSFTP;
|
||||
|
||||
class PresupuestoUploaderService
|
||||
{
|
||||
public function __construct(
|
||||
protected SftpClientWrapper $ftp,
|
||||
protected PresupuestoFicheroModel $fileModel,
|
||||
protected PresupuestoSFTP $config
|
||||
) {}
|
||||
|
||||
public function uploadToRemote(int $presupuestoId): array
|
||||
{
|
||||
$remoteDir = "{$this->config->base_dir}/ficheros/" . ($presupuestoId + $this->config->id_offset);
|
||||
|
||||
if (!$this->ftp->exists($remoteDir)) {
|
||||
if (!$this->ftp->mkdir($remoteDir, true)) {
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => "No se pudo crear el directorio remoto: $remoteDir"
|
||||
];
|
||||
}
|
||||
$this->ftp->chmod($remoteDir, 0755);
|
||||
}
|
||||
|
||||
$files = $this->fileModel->getFiles($presupuestoId);
|
||||
$results = [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$remotePath = $remoteDir . '/' . basename($file->file_path);
|
||||
$localPath = WRITEPATH . $file->file_path;
|
||||
$ok = $this->ftp->upload($localPath, $remotePath);
|
||||
|
||||
$results[] = [
|
||||
'file' => $file->nombre,
|
||||
'remotePath' => $remotePath,
|
||||
'success' => $ok
|
||||
];
|
||||
}
|
||||
|
||||
$allOk = !in_array(false, array_column($results, 'success'));
|
||||
|
||||
return [
|
||||
'success' => $allOk,
|
||||
'files' => $results
|
||||
];
|
||||
}
|
||||
|
||||
public function removeFromRemote(int $presupuestoId): void
|
||||
{
|
||||
$remoteDir = "{$this->config->base_dir}/pedidos_files/" . ($presupuestoId + $this->config->id_offset);
|
||||
$files = $this->fileModel->getFiles($presupuestoId);
|
||||
|
||||
foreach ($files as $file) {
|
||||
$this->ftp->delete($remoteDir . '/' . basename($file->file_path));
|
||||
}
|
||||
}
|
||||
|
||||
public function removeMissingFromRemote(int $presupuestoId, array $keepFileNames): void
|
||||
{
|
||||
$remoteDir = "{$this->config->base_dir}/pedidos_files/" . ($presupuestoId + $this->config->id_offset);
|
||||
$files = $this->fileModel->getFiles($presupuestoId);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if (!in_array($file->nombre, $keepFileNames)) {
|
||||
$this->ftp->delete($remoteDir . '/' . basename($file->file_path));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user