fn_b/app/Services/SiteMode.php

41 lines
1.6 KiB
PHP

<?php
namespace App\Services;
use App\Models\Articles;
use App\Models\Categories;
use Illuminate\Http\Exceptions\HttpResponseException;
final class SiteMode
{
public static function quiet(): bool
{
$path = config('site_mode.state_path');
if (! $path || ! is_file($path)) return (bool) config('site_mode.quiet', false);
$state = json_decode((string) file_get_contents($path), true);
// An unreadable/invalid operational flag must not silently resume publishing.
return ! is_array($state) || ! is_bool($state['quiet'] ?? null) || $state['quiet'];
}
public static function assertUpdatesAllowed(?string $type): void
{
if ($type !== 'freenode' || ! self::quiet()) return;
throw new HttpResponseException(response()->json([
'isDone' => false, 'status' => 'paused', 'code' => 'FREE_NODE_UPDATES_PAUSED',
'message' => 'Free node updates are temporarily paused; existing publications are retained.',
], 503, ['Retry-After' => '3600']));
}
public static function guardArticle(Articles $article): void
{
if (! self::quiet()) return;
$fields = ['sSlug','iCategoryId','sTitle','sTitleSub','sContent','sThumb',
'iStatus','created_at','sDisplayPublishedAt','sResourceStatus','iResourceCount'];
if ($article->exists && ! $article->isDirty($fields)) return;
$ids = array_filter([$article->iCategoryId, $article->getRawOriginal('iCategoryId')]);
if ($ids && Categories::whereIn('id', $ids)->where('sSlug', 'freenode')->exists()) {
self::assertUpdatesAllowed('freenode');
}
}
}