Passed
Pull Request — master (#138)
by
unknown
24:26 queued 09:22
created

PostController::edit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 21
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 32
rs 9.584
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Post;
6
7
use App\Blog\Entity\Post;
8
9
use App\Service\UserService;
10
use App\ViewRenderer;
11
use App\Service\WebControllerService;
12
use Psr\Http\Message\ResponseInterface as Response;
13
use Psr\Http\Message\ServerRequestInterface as Request;
14
use Yiisoft\Http\Method;
15
16
final class PostController
17
{
18
    private ViewRenderer $viewRenderer;
19
    private WebControllerService $webService;
20
    private PostService $postService;
21
    private UserService $userService;
22
23
    public function __construct(
24
        ViewRenderer $viewRenderer,
25
        WebControllerService $webService,
26
        PostService $postService,
27
        UserService $userService
28
    ) {
29
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
30
        $this->webService = $webService;
31
        $this->postService = $postService;
32
        $this->userService = $userService;
33
    }
34
35
    public function index(Request $request, PostRepository $postRepository): Response
36
    {
37
        $canEdit = $this->userService->hasPermission('editPost');
38
        $slug = $request->getAttribute('slug', null);
39
        $item = $postRepository->fullPostPage($slug);
40
        if ($item === null) {
41
            return $this->webService->getNotFoundResponse();
42
        }
43
44
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
45
    }
46
47
    public function add(Request $request, PostForm $form): Response
48
    {
49
        $parameters = [
50
            'title' => 'Add post',
51
            'action' => ['blog/add'],
52
            'errors' => [],
53
            'body' => $request->getParsedBody(),
54
        ];
55
56
        if ($request->getMethod() === Method::POST) {
57
            $form->load($parameters['body']);
0 ignored issues
show
Bug introduced by
It seems like $parameters['body'] can also be of type null and object; however, parameter $data of Yiisoft\Form\FormModel::load() does only seem to accept array, 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

57
            $form->load(/** @scrutinizer ignore-type */ $parameters['body']);
Loading history...
58
            if ($form->validate()) {
59
                $this->postService->savePost($this->userService->getUser(), new Post(), $form);
60
                return $this->webService->getRedirectResponse('blog/index');
61
            }
62
63
            $parameters['errors'] = $form->firstErrors();
64
        }
65
66
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
67
    }
68
69
    public function edit(Request $request, PostForm $form, PostRepository $postRepository): Response
70
    {
71
        $slug = $request->getAttribute('slug', null);
72
        $post = $postRepository->fullPostPage($slug);
73
        if ($post === null) {
74
            return $this->webService->getNotFoundResponse();
75
        }
76
77
        $parameters = [
78
            'title' => 'Edit post',
79
            'action' => ['blog/edit', ['slug' => $slug]],
80
            'errors' => [],
81
            'body' => [
82
                'title' => $post->getTitle(),
83
                'content' => $post->getContent(),
84
                'tags' => $this->postService->getPostTags($post)
85
            ]
86
        ];
87
88
        if ($request->getMethod() === Method::POST) {
89
            $body = $request->getParsedBody();
90
            $form->load($body);
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type null and object; however, parameter $data of Yiisoft\Form\FormModel::load() does only seem to accept array, 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

90
            $form->load(/** @scrutinizer ignore-type */ $body);
Loading history...
91
            if ($form->validate()) {
92
                $this->postService->savePost($this->userService->getUser(), $post, $form);
93
                return $this->webService->getRedirectResponse('blog/index');
94
            }
95
96
            $parameters['body'] = $body;
97
            $parameters['errors'] = $form->firstErrors();
98
        }
99
100
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
101
    }
102
}
103