Issues (39)

src/Blog/Post/PostService.php (1 issue)

Labels
Severity
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
It seems like $form->getContent() can also be of type null; however, parameter $content of App\Blog\Entity\Post::setContent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $model->setContent(/** @scrutinizer ignore-type */ $form->getContent());
Loading history...
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