Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

PostController::index()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Blog;
6
7
use App\Http\Service\WebControllerService;
8
use App\Modules\Blog\Entity\Post;
9
use App\Modules\Blog\Post\PostForm;
10
use App\Modules\Blog\Post\PostRepository;
11
use App\Modules\Blog\Post\PostService;
12
use App\Modules\User\UserService;
13
use Psr\Http\Message\ResponseInterface as Response;
14
use Psr\Http\Message\ServerRequestInterface as Request;
15
use Yiisoft\Http\Method;
16
use Yiisoft\Router\CurrentRoute;
17
use Yiisoft\Validator\ValidatorInterface;
18
use Yiisoft\Yii\View\ViewRenderer;
19
20
final class PostController
21
{
22
    public function __construct(
23
        private WebControllerService $webService,
24
        private PostService $postService,
25
        private UserService $userService,
26
        private ViewRenderer $viewRenderer,
27
    ) {
28
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
29
    }
30
31
    public function index(CurrentRoute $currentRoute, PostRepository $postRepository): Response
32
    {
33
        $canEdit = $this->userService->hasPermission('editPost');
34
        $slug = $currentRoute->getArgument('slug');
35
        $item = $postRepository->fullPostPage($slug);
0 ignored issues
show
Bug introduced by
It seems like $slug can also be of type null; however, parameter $slug of App\Modules\Blog\Post\Po...ository::fullPostPage() 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

35
        $item = $postRepository->fullPostPage(/** @scrutinizer ignore-type */ $slug);
Loading history...
36
        if ($item === null) {
37
            return $this->webService->getNotFoundResponse();
38
        }
39
40
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
41
    }
42
43
    public function add(Request $request, ValidatorInterface $validator): Response
44
    {
45
        $parameters = [
46
            'title' => 'Add post',
47
            'action' => ['blog/add'],
48
            'errors' => [],
49
            'body' => $request->getParsedBody(),
50
        ];
51
52
        if ($request->getMethod() === Method::POST) {
53
            $form = new PostForm();
54
            if ($form->load($parameters['body']) && $validator
55
                    ->validate($form)
56
                    ->isValid()) {
57
                $this->postService->savePost($this->userService->getUser(), new Post(), $form);
0 ignored issues
show
Bug introduced by
It seems like $this->userService->getUser() can also be of type null; however, parameter $user of App\Modules\Blog\Post\PostService::savePost() does only seem to accept App\Modules\User\User, 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
                $this->postService->savePost(/** @scrutinizer ignore-type */ $this->userService->getUser(), new Post(), $form);
Loading history...
58
                return $this->webService->getRedirectResponse('blog/index');
59
            }
60
61
            $parameters['errors'] = $form->getFirstErrors();
62
        }
63
64
        return $this->viewRenderer->render('__form', $parameters);
65
    }
66
67
    public function edit(
68
        Request $request,
69
        PostRepository $postRepository,
70
        ValidatorInterface $validator,
71
        CurrentRoute $currentRoute
72
    ): Response {
73
        $slug = $currentRoute->getArgument('slug');
74
        $post = $postRepository->fullPostPage($slug);
0 ignored issues
show
Bug introduced by
It seems like $slug can also be of type null; however, parameter $slug of App\Modules\Blog\Post\Po...ository::fullPostPage() 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

74
        $post = $postRepository->fullPostPage(/** @scrutinizer ignore-type */ $slug);
Loading history...
75
        if ($post === null) {
76
            return $this->webService->getNotFoundResponse();
77
        }
78
79
        $parameters = [
80
            'title' => 'Edit post',
81
            'action' => ['blog/edit', ['slug' => $slug]],
82
            'errors' => [],
83
            'body' => [
84
                'title' => $post->getTitle(),
85
                'content' => $post->getContent(),
86
                'tags' => $this->postService->getPostTags($post),
87
            ],
88
        ];
89
90
        if ($request->getMethod() === Method::POST) {
91
            $form = new PostForm();
92
            $body = $request->getParsedBody();
93
            if ($form->load($body) && $validator
94
                    ->validate($form)
95
                    ->isValid()) {
96
                $this->postService->savePost($this->userService->getUser(), $post, $form);
0 ignored issues
show
Bug introduced by
It seems like $this->userService->getUser() can also be of type null; however, parameter $user of App\Modules\Blog\Post\PostService::savePost() does only seem to accept App\Modules\User\User, 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

96
                $this->postService->savePost(/** @scrutinizer ignore-type */ $this->userService->getUser(), $post, $form);
Loading history...
97
                return $this->webService->getRedirectResponse('blog/index');
98
            }
99
100
            $parameters['body'] = $body;
101
            $parameters['errors'] = $form->getFirstErrors();
102
        }
103
104
        return $this->viewRenderer->render('__form', $parameters);
105
    }
106
}
107