Review, publish, preview and undo changes through the API
Revisions now call publishing only what publishes: a project's status is whether the work is active, and a service is published by its flag. The fake identity server answers by token, so a test can sign in a second person. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
132
app/Support/AdminContent.php
Normal file
132
app/Support/AdminContent.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Call;
|
||||
use App\Models\Category;
|
||||
use App\Models\Client;
|
||||
use App\Models\Member;
|
||||
use App\Models\Page;
|
||||
use App\Models\Project;
|
||||
use App\Models\Service;
|
||||
use App\Models\Talk;
|
||||
use App\Models\Technology;
|
||||
use App\Models\Testimonial;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
/**
|
||||
* The content the admin API addresses by name — `pages`, `projects` — and
|
||||
* what each one supports beyond plain editing: a history, a review, a preview.
|
||||
*
|
||||
* One table rather than a flag on every model, so the routes, the
|
||||
* specification and the controllers that serve those extras all read the same
|
||||
* answer.
|
||||
*/
|
||||
final class AdminContent
|
||||
{
|
||||
/**
|
||||
* API name => [model, route parameter].
|
||||
*
|
||||
* @var array<string, array{0: class-string<Model>, 1: string}>
|
||||
*/
|
||||
public const WITH_HISTORY = [
|
||||
'pages' => [Page::class, 'page'],
|
||||
'categories' => [Category::class, 'category'],
|
||||
'projects' => [Project::class, 'project'],
|
||||
'members' => [Member::class, 'member'],
|
||||
'services' => [Service::class, 'service'],
|
||||
'clients' => [Client::class, 'client'],
|
||||
'testimonials' => [Testimonial::class, 'testimonial'],
|
||||
'talks' => [Talk::class, 'talk'],
|
||||
'technologies' => [Technology::class, 'technology'],
|
||||
'calls' => [Call::class, 'call'],
|
||||
];
|
||||
|
||||
/**
|
||||
* What goes through a review before it reaches the site, and the fields
|
||||
* that put each one there — the ones `{resource}.publish` guards.
|
||||
*
|
||||
* @var array<string, array<int, string>>
|
||||
*/
|
||||
public const REVIEWABLE = [
|
||||
'pages' => ['status', 'published_at', 'unpublish_at'],
|
||||
'projects' => ['published_at', 'unpublish_at'],
|
||||
'services' => ['is_published', 'published_at', 'unpublish_at'],
|
||||
];
|
||||
|
||||
/**
|
||||
* What has an address on the public site to preview, by the type name
|
||||
* `SiteUrls` knows it as.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
public const PREVIEWABLE = [
|
||||
'pages' => 'page',
|
||||
'projects' => 'project',
|
||||
'services' => 'service',
|
||||
'calls' => 'call',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return class-string<Model>
|
||||
*/
|
||||
public static function model(string $resource): string
|
||||
{
|
||||
return self::WITH_HISTORY[$resource][0] ?? abort(404);
|
||||
}
|
||||
|
||||
public static function parameter(string $resource): string
|
||||
{
|
||||
return self::WITH_HISTORY[$resource][1] ?? abort(404);
|
||||
}
|
||||
|
||||
/**
|
||||
* The record a route names, binned ones included: their history is still
|
||||
* worth reading, and restoring them is what the bin is for.
|
||||
*/
|
||||
public static function find(string $resource, mixed $id): Model
|
||||
{
|
||||
return self::findOf(self::model($resource), $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param class-string<Model> $model
|
||||
*/
|
||||
public static function findOf(string $model, mixed $id): Model
|
||||
{
|
||||
$query = $model::query();
|
||||
|
||||
if (in_array(SoftDeletes::class, class_uses_recursive($model), true)) {
|
||||
$query->withTrashed();
|
||||
}
|
||||
|
||||
return $query->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* The API name of a record, for a review queue that mixes several kinds.
|
||||
*/
|
||||
public static function nameOf(Model $record): ?string
|
||||
{
|
||||
foreach (self::WITH_HISTORY as $resource => [$model]) {
|
||||
if ($record instanceof $model) {
|
||||
return $resource;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The fields only `{resource}.publish` may write, for this record.
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function publicationFields(Model $record): array
|
||||
{
|
||||
return self::REVIEWABLE[self::nameOf($record)] ?? [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user