PostService::savePost()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 3
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
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
    public function __construct(private PostRepository $repository, private TagRepository $tagRepository)
15
    {
16
    }
17
18
    public function savePost(User $user, Post $model, PostForm $form): void
19
    {
20
        $model->setTitle($form->getTitle());
21
        $model->setContent($form->getContent());
0 ignored issues
show
Bug introduced by
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

21
        $model->setContent(/** @scrutinizer ignore-type */ $form->getContent());
Loading history...
22
        $model->resetTags();
23
24
        foreach ($form->getTags() as $tag) {
25
            $model->addTag($this->tagRepository->getOrCreate($tag));
26
        }
27
28
        if ($model->isNewRecord()) {
29
            $model->setPublic(true);
30
            $model->setUser($user);
31
        }
32
33
        $this->repository->save($model);
34
    }
35
36
    public function getPostTags(Post $post): array
37
    {
38
        return array_map(
39
            static fn (Tag $tag) => $tag->getLabel(),
40
            $post->getTags()
41
        );
42
    }
43
}
44