42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Post\UseCases\DeletePost;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\Exceptions\ForbiddenException;
|
|
use App\Post\PostRepository;
|
|
|
|
class DeletePost
|
|
{
|
|
public function __construct(
|
|
private PostRepository $postRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
* @throws ForbiddenException
|
|
*/
|
|
public function execute(DeletePostRequest $request): void
|
|
{
|
|
if ($request->postId <= 0) {
|
|
throw new BadRequestException('postId must be positive');
|
|
}
|
|
if ($request->requesterId <= 0) {
|
|
throw new BadRequestException('requesterId must be positive');
|
|
}
|
|
|
|
$post = $this->postRepo->find($request->postId);
|
|
if ($post === null) {
|
|
return;
|
|
}
|
|
|
|
$isAuthor = $post->getUserId() === $request->requesterId;
|
|
if (! $isAuthor && ! $request->requesterIsAdmin) {
|
|
throw new ForbiddenException(
|
|
'requester is not allowed to delete this post'
|
|
);
|
|
}
|
|
|
|
$this->postRepo->delete($request->postId);
|
|
}
|
|
}
|