netgescon-day0/app/Console/Commands/FetchMailboxesCommand.php

99 lines
3.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Stabile;
use App\Services\Posta\ImapMailboxService;
use Illuminate\Console\Command;
class FetchMailboxesCommand extends Command
{
protected $signature = 'gescon:fetch-mail
{--stabile= : Codice o ID stabile (es. 0013, 0021)}
{--tipo= : Filtra per tipo (email, pec, gmail)}
{--max=30 : Massimo numero di messaggi per casella}';
protected $description = 'Scarica la posta elettronica e PEC via IMAP/Gmail per gli stabili configurati e genera i file .EML e comunicazioni';
public function handle(ImapMailboxService $service): int
{
$this->info('======================================================================');
$this->info(' NETGESCON: SCARICO POSTA & PEC VIA PROTOCOLLO IMAP ');
$this->info('======================================================================');
$stabileFilter = $this->option('stabile');
$tipoFilter = strtolower((string) $this->option('tipo'));
$max = (int) $this->option('max') ?: 30;
$query = Stabile::where('attivo', true);
if ($stabileFilter) {
$query->where(function ($q) use ($stabileFilter) {
$q->where('codice_stabile', $stabileFilter)
->orWhere('cod_stabile', $stabileFilter)
->orWhere('id', $stabileFilter);
});
}
$stabili = $query->get();
if ($stabili->isEmpty()) {
$this->warn('Nessuno stabile attivo trovato con i filtri specificati.');
return Command::SUCCESS;
}
$totImported = 0;
$totSkipped = 0;
$totErrors = 0;
foreach ($stabili as $stabile) {
$config = (array) ($stabile->configurazione_avanzata ?? []);
$posta = (array) ($config['posta'] ?? []);
$caselle = array_values(array_filter((array) ($posta['caselle'] ?? []), 'is_array'));
if (empty($caselle)) {
continue;
}
$this->line("\nElaborazione Stabile [{$stabile->codice_stabile}] {$stabile->denominazione} (" . count($caselle) . " caselle)");
foreach ($caselle as $idx => $mailbox) {
if (empty($mailbox['enabled'])) {
continue;
}
$tipo = strtolower(trim((string) ($mailbox['tipo'] ?? 'imap')));
if ($tipoFilter && $tipo !== $tipoFilter) {
continue;
}
$label = $mailbox['label'] ?: ($mailbox['email'] ?: "Casella " . ($idx + 1));
$this->line(" -> Scansione casella [{$tipo}]: {$label} ({$mailbox['email']})");
if ($tipo === 'gmail') {
$this->comment(" [Gmail API]: in attesa di autorizzazione sviluppatore Google. Usare IMAP con password applicazione.");
}
if (! empty($mailbox['host']) && ! empty($mailbox['username'])) {
$res = $service->fetchMailbox($stabile, $mailbox, $max);
$this->info(" Importati: {$res['imported']} | Duplicati: {$res['skipped']} | Errori: {$res['errors']}");
$totImported += $res['imported'];
$totSkipped += $res['skipped'];
$totErrors += $res['errors'];
}
}
}
$this->newLine();
$this->info("Riepilogo finale scarico posta:");
$this->table(
['Metrica', 'Valore'],
[
['Messaggi importati a nuovo', $totImported],
['Messaggi già presenti (duplicati)', $totSkipped],
['Errori di sincronizzazione', $totErrors],
]
);
return Command::SUCCESS;
}
}