1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace App\Blog\Post; |
||
6 | |||
7 | use App\Blog\Entity\Post; |
||
8 | use App\Blog\Entity\Tag; |
||
9 | use App\Blog\Tag\TagRepository; |
||
10 | use App\User\User; |
||
11 | |||
12 | final class PostService |
||
13 | { |
||
14 | private PostRepository $repository; |
||
15 | private TagRepository $tagRepository; |
||
16 | |||
17 | public function __construct(PostRepository $repository, TagRepository $tagRepository) |
||
18 | { |
||
19 | $this->repository = $repository; |
||
20 | $this->tagRepository = $tagRepository; |
||
21 | } |
||
22 | |||
23 | public function savePost(User $user, Post $model, PostForm $form): void |
||
24 | { |
||
25 | $model->setTitle($form->getTitle()); |
||
26 | $model->setContent($form->getContent()); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
27 | $model->resetTags(); |
||
28 | |||
29 | foreach ($form->getTags() as $tag) { |
||
30 | $model->addTag($this->tagRepository->getOrCreate($tag)); |
||
31 | } |
||
32 | |||
33 | if ($model->isNewRecord()) { |
||
34 | $model->setPublic(true); |
||
35 | $model->setUser($user); |
||
36 | } |
||
37 | |||
38 | $this->repository->save($model); |
||
39 | } |
||
40 | |||
41 | public function getPostTags(Post $post): array |
||
42 | { |
||
43 | return array_map( |
||
44 | static fn (Tag $tag) => $tag->getLabel(), |
||
45 | $post->getTags() |
||
46 | ); |
||
47 | } |
||
48 | } |
||
49 |