67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Integrations\Whmcs\Support;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Container\Attributes\Singleton;
|
|
|
|
#[Singleton]
|
|
class SyncReport
|
|
{
|
|
/** @var array<int, array{entity: string, ref: string, whmcs_id: string, issue: string, detail: string}> */
|
|
private array $notes = [];
|
|
|
|
private bool $batch = false;
|
|
|
|
public function note(string $entity, string $ref, int|string $whmcsId, string $issue, string $detail): void
|
|
{
|
|
$this->notes[] = ['entity' => $entity, 'ref' => $ref, 'whmcs_id' => (string) $whmcsId, 'issue' => $issue, 'detail' => $detail];
|
|
}
|
|
|
|
public function beginBatch(): void
|
|
{
|
|
$this->batch = true;
|
|
}
|
|
|
|
public function finishBatch(Command $command): void
|
|
{
|
|
$this->batch = false;
|
|
$this->finish($command);
|
|
}
|
|
|
|
public function finish(Command $command): void
|
|
{
|
|
if ($this->batch || $this->notes === []) {
|
|
return;
|
|
}
|
|
|
|
$summary = [];
|
|
foreach ($this->notes as $note) {
|
|
$key = $note['entity'] . ' / ' . $note['issue'];
|
|
$summary[$key] = ($summary[$key] ?? 0) + 1;
|
|
}
|
|
ksort($summary);
|
|
|
|
$command->newLine();
|
|
$command->warn('Ghi chú đồng bộ (' . count($this->notes) . ' mục):');
|
|
$command->table(['Loại / vấn đề', 'Số lượng'], array_map(fn ($key, $count) => [$key, $count], array_keys($summary), $summary));
|
|
$command->info('Chi tiết: ' . $this->writeCsv());
|
|
|
|
$this->notes = [];
|
|
}
|
|
|
|
private function writeCsv(): string
|
|
{
|
|
$path = storage_path('logs/whmcs-sync-report-' . now()->format('Ymd-His-u') . '.csv');
|
|
$handle = fopen($path, 'w');
|
|
fwrite($handle, "\xEF\xBB\xBF");
|
|
fputcsv($handle, ['entity', 'ref', 'whmcs_id', 'issue', 'detail']);
|
|
foreach ($this->notes as $note) {
|
|
fputcsv($handle, $note);
|
|
}
|
|
fclose($handle);
|
|
|
|
return $path;
|
|
}
|
|
}
|