Passed
Pull Request — master (#138)
by Sergei
11:25
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
use App\Service\UserService;
9
use App\Service\WebControllerService;
10
use Psr\Http\Message\ResponseInterface as Response;
11
use Psr\Http\Message\ServerRequestInterface as Request;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Yii\View\ViewRenderer;
14
15
final class PostController
16
{
17
    private ViewRenderer $viewRenderer;
18
    private WebControllerService $webService;
19
    private PostService $postService;
20
    private UserService $userService;
21
22
    public function __construct(
23
        ViewRenderer $viewRenderer,
24
        WebControllerService $webService,
25
        PostService $postService,
26
        UserService $userService
27
    ) {
28
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
29
        $this->webService = $webService;
30
        $this->postService = $postService;
31
        $this->userService = $userService;
32
    }
33
34
    public function index(Request $request, PostRepository $postRepository): Response
35
    {
36
        $canEdit = $this->userService->hasPermission('editPost');
37
        $slug = $request->getAttribute('slug', null);
38
        $item = $postRepository->fullPostPage($slug);
39
        if ($item === null) {
40
            return $this->webService->getNotFoundResponse();
41
        }
42
43
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
44
    }
45
46
    public function add(Request $request, PostForm $form): Response
47
    {
48
        $parameters = [
49
            'title' => 'Add post',
50
            'action' => ['blog/add'],
51
            'errors' => [],
52
            'body' => $request->getParsedBody(),
53
        ];
54
55
        if ($request->getMethod() === Method::POST) {
56
            $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

56
            $form->load(/** @scrutinizer ignore-type */ $parameters['body']);
Loading history...
57
            if ($form->validate()) {
58
                $this->postService->savePost($this->userService->getUser(), new Post(), $form);
59
                return $this->webService->getRedirectResponse('blog/index');
60
            }
61
62
            $parameters['errors'] = $form->firstErrors();
63
        }
64
65
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
66
    }
67
68
    public function edit(Request $request, PostForm $form, PostRepository $postRepository): Response
69
    {
70
        $slug = $request->getAttribute('slug', null);
71
        $post = $postRepository->fullPostPage($slug);
72
        if ($post === null) {
73
            return $this->webService->getNotFoundResponse();
74
        }
75
76
        $parameters = [
77
            'title' => 'Edit post',
78
            'action' => ['blog/edit', ['slug' => $slug]],
79
            'errors' => [],
80
            'body' => [
81
                'title' => $post->getTitle(),
82
                'content' => $post->getContent(),
83
                'tags' => $this->postService->getPostTags($post)
84
            ]
85
        ];
86
87
        if ($request->getMethod() === Method::POST) {
88
            $body = $request->getParsedBody();
89
            $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

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