48 lines
1.2 KiB
PHP
48 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Post\UseCases\ClearFeaturedPost;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\ForbiddenException;
|
|
use App\Post\Post;
|
|
use App\Post\PostRepository;
|
|
|
|
class ClearFeaturedPost
|
|
{
|
|
public function __construct(
|
|
private PostRepository $postRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws ForbiddenException
|
|
*/
|
|
public function execute(ClearFeaturedPostRequest $request): void
|
|
{
|
|
if (! $request->requesterIsAdmin) {
|
|
throw new ForbiddenException(
|
|
'only admins can unfeature a post'
|
|
);
|
|
}
|
|
if ($request->postId <= 0) {
|
|
throw new BadRequestException('postId must be positive');
|
|
}
|
|
|
|
$post = $this->postRepo->find($request->postId);
|
|
if ($post === null) {
|
|
return;
|
|
}
|
|
if (! $post->isFeatured()) {
|
|
return;
|
|
}
|
|
|
|
$this->postRepo->update(new Post(
|
|
id: $post->getId(),
|
|
userId: $post->getUserId(),
|
|
title: $post->getTitle(),
|
|
body: $post->getBody(),
|
|
createdAt: $post->getCreatedAt(),
|
|
featureSlot: null,
|
|
));
|
|
}
|
|
}
|