Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions app/Console/Commands/CertificatePreflight.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,31 @@ public function handle(): int
$totalTested = 0;
$totalPassed = 0;
$offset = 0;
$exportFh = null;
if ($exportPath !== '') {
$path = $this->resolvePath($exportPath);
$dir = dirname($path);
if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->error("Failed to create export directory: {$dir}");
return self::FAILURE;
}
$exportFh = @fopen($path, 'wb');
if (! $exportFh) {
$this->error("Failed to open export file: {$path}");
return self::FAILURE;
}
fputcsv($exportFh, ['id', 'type', 'edition', 'user_id', 'email', 'name_for_certificate', 'error']);
}

while ($offset < $totalToTest) {
$failuresBeforeBatch = count($allFailures);
$take = min($batchSize, $totalToTest - $offset);
$query = (clone $baseQuery)->offset($offset)->limit($take);
$rows = $query->get();
if ($rows->isEmpty()) {
break;
}

$batchNum = (int) floor($offset / $batchSize) + 1;
$bar = $this->output->createProgressBar($rows->count());
$bar->setFormat(" Batch %current%/%max% [%bar%] %percent:3s%% — failures this batch: ");
$bar->start();
Expand Down Expand Up @@ -125,6 +140,21 @@ public function handle(): int
$bar->finish();
$totalTested += $rows->count();
$this->line(" {$batchFailures} | Total: {$totalTested}/{$totalToTest} tested, " . count($allFailures) . " failures.");
if ($exportFh !== null) {
$batchFailureRows = array_slice($allFailures, $failuresBeforeBatch);
foreach ($batchFailureRows as $row) {
fputcsv($exportFh, [
$row['id'],
$row['type'],
$edition,
$row['user_id'],
$row['email'],
$row['name'],
$row['error'],
]);
}
fflush($exportFh);
}
$offset += $rows->count();
}

Expand All @@ -139,31 +169,9 @@ public function handle(): int
}
}

if ($exportPath !== '') {
if ($exportFh !== null) {
fclose($exportFh);
$path = $this->resolvePath($exportPath);
$dir = dirname($path);
if (! is_dir($dir) && ! @mkdir($dir, 0775, true) && ! is_dir($dir)) {
$this->error("Failed to create export directory: {$dir}");
return self::FAILURE;
}
$fh = @fopen($path, 'wb');
if (! $fh) {
$this->error("Failed to open export file: {$path}");
return self::FAILURE;
}
fputcsv($fh, ['id', 'type', 'edition', 'user_id', 'email', 'name_for_certificate', 'error']);
foreach ($allFailures as $row) {
fputcsv($fh, [
$row['id'],
$row['type'],
$edition,
$row['user_id'],
$row['email'],
$row['name'],
$row['error'],
]);
}
fclose($fh);
$this->info(empty($allFailures) ? "Exported (no failures): {$path}" : "Exported failures only: {$path}");
}

Expand Down
Loading