cms/app/Services/PostService.php

58 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Services;
use App\Models\CustomPostType;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
class PostService
{
/**
* Store a newly created post.
*
* @param CustomPostType $customPostType
* @param array $data
* @return Post
*/
public function storePost(CustomPostType $customPostType, array $data): Post
{
$data['custom_post_type_id'] = $customPostType->id;
$data['user_id'] = Auth::id();
if ($data['status'] === 'published' && empty($data['published_at'])) {
$data['published_at'] = now();
}
return Post::create($data);
}
/**
* Update the specified post.
*
* @param Post $post
* @param array $data
* @return Post
*/
public function updatePost(Post $post, array $data): Post
{
if ($data['status'] === 'published' && !$post->published_at && !$data['published_at']) {
$data['published_at'] = now();
}
$post->update($data);
return $post;
}
/**
* Remove the specified post.
*
* @param Post $post
* @return bool
*/
public function deletePost(Post $post): bool
{
return (bool) $post->delete();
}
}