- Removed the PostStatusGuard class and replaced its usage with the new PostStatusRules utility across multiple controllers and actions, enhancing code organization and maintainability. - Updated error message handling to utilize the centralized method in PostStatusRules, ensuring consistency in user feedback. - Deleted associated tests for PostStatusGuard, reflecting the removal of the class.
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Enums\Post\Status as PostStatus;
|
|
use App\Models\Post;
|
|
|
|
class PostStatusRules
|
|
{
|
|
private const EDIT_BLOCKED_MESSAGE_KEY = 'posts.cannot_edit_finalized';
|
|
|
|
/**
|
|
* Statuses where the post can no longer be edited.
|
|
*
|
|
* @var array<int, PostStatus>
|
|
*/
|
|
private const EDIT_BLOCKED_STATUSES = [
|
|
PostStatus::Published,
|
|
PostStatus::PartiallyPublished,
|
|
PostStatus::Failed,
|
|
PostStatus::Publishing,
|
|
];
|
|
|
|
/**
|
|
* Statuses where the post can no longer be deleted.
|
|
*
|
|
* @var array<int, PostStatus>
|
|
*/
|
|
private const DELETE_BLOCKED_STATUSES = [
|
|
PostStatus::Publishing,
|
|
PostStatus::Published,
|
|
PostStatus::PartiallyPublished,
|
|
];
|
|
|
|
public static function blocksEditing(Post $post): bool
|
|
{
|
|
return in_array($post->status, self::EDIT_BLOCKED_STATUSES, true);
|
|
}
|
|
|
|
public static function blocksDeletion(Post $post): bool
|
|
{
|
|
return in_array($post->status, self::DELETE_BLOCKED_STATUSES, true);
|
|
}
|
|
|
|
public static function editBlockedMessage(): string
|
|
{
|
|
return __(self::EDIT_BLOCKED_MESSAGE_KEY);
|
|
}
|
|
}
|