108 lines
3.3 KiB
PHP
108 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\PressReleaseStatus;
|
|
use App\Models\PressRelease;
|
|
use App\Services\PressRelease\BlacklistViolationException;
|
|
use App\Services\PressRelease\PressReleaseService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Str;
|
|
use Throwable;
|
|
|
|
/**
|
|
* Veröffentlicht Pressemitteilungen mit Status `review` und einem
|
|
* `scheduled_at`-Zeitpunkt, der erreicht/überschritten wurde.
|
|
*
|
|
* Läuft regelmäßig per Scheduler (siehe routes/console.php). Idempotent:
|
|
* berührt nur PRs in Review-Status — bereits publishte werden ignoriert.
|
|
*
|
|
* Blacklist-Treffer landen wie beim manuellen Publish im Reject-Status mit
|
|
* Mail-Benachrichtigung des Autors.
|
|
*/
|
|
class PublishScheduledPressReleases extends Command
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $signature = 'press-releases:publish-scheduled
|
|
{--dry-run : Nur anzeigen, was publiziert würde, ohne DB zu ändern}
|
|
{--limit=200 : Maximale Anzahl pro Lauf}';
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $description = 'Veröffentlicht fällige geplante Pressemitteilungen (Status review + scheduled_at <= now).';
|
|
|
|
public function handle(PressReleaseService $service): int
|
|
{
|
|
$dryRun = (bool) $this->option('dry-run');
|
|
$limit = max(1, (int) $this->option('limit'));
|
|
|
|
$now = now();
|
|
|
|
$candidates = PressRelease::withoutGlobalScopes()
|
|
->where('status', PressReleaseStatus::Review->value)
|
|
->whereNotNull('scheduled_at')
|
|
->where('scheduled_at', '<=', $now)
|
|
->orderBy('scheduled_at')
|
|
->limit($limit)
|
|
->get();
|
|
|
|
if ($candidates->isEmpty()) {
|
|
$this->info('Keine fälligen geplanten Pressemitteilungen gefunden.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->info(sprintf(
|
|
'%d fällige Pressemitteilung(en) gefunden.%s',
|
|
$candidates->count(),
|
|
$dryRun ? ' (Dry-Run)' : '',
|
|
));
|
|
|
|
$published = 0;
|
|
$rejected = 0;
|
|
$failed = 0;
|
|
|
|
foreach ($candidates as $pressRelease) {
|
|
$line = sprintf(
|
|
' #%d scheduled_at=%s title="%s"',
|
|
$pressRelease->id,
|
|
$pressRelease->scheduled_at?->format('Y-m-d H:i') ?? '-',
|
|
Str::limit($pressRelease->title, 60),
|
|
);
|
|
|
|
if ($dryRun) {
|
|
$this->line($line.' [DRY]');
|
|
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
$service->publish($pressRelease, source: 'scheduler');
|
|
$published++;
|
|
$this->line($line.' [OK]');
|
|
} catch (BlacklistViolationException $e) {
|
|
$rejected++;
|
|
$this->warn($line.' [REJECT: '.$e->word.']');
|
|
} catch (Throwable $e) {
|
|
$failed++;
|
|
$this->error($line.' [FAIL: '.$e->getMessage().']');
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
if (! $dryRun) {
|
|
$this->newLine();
|
|
$this->info(sprintf(
|
|
'Fertig: %d veröffentlicht, %d wegen Blacklist abgelehnt, %d fehlgeschlagen.',
|
|
$published,
|
|
$rejected,
|
|
$failed,
|
|
));
|
|
}
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|