PostController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 10 2
A __construct() 0 7 1
A add() 0 21 3
A edit() 0 36 4
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\WebControllerService;
9
use App\User\UserService;
10
use Psr\Http\Message\ResponseInterface as Response;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Http\Message\ServerRequestInterface as Request;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\FormModel\FormHydrator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormHydrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\Http\Method;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Method was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Router\CurrentRoute;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Router\CurrentRoute was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Yii\View\Renderer\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\Renderer\ViewRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
final class PostController
18
{
19
    public function __construct(
20
        private WebControllerService $webService,
21
        private PostService $postService,
22
        private UserService $userService,
23
        private ViewRenderer $viewRenderer,
24
    ) {
25
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
26
    }
27
28
    public function index(CurrentRoute $currentRoute, PostRepository $postRepository): Response
29
    {
30
        $canEdit = $this->userService->hasPermission('editPost');
31
        $slug = $currentRoute->getArgument('slug');
32
        $item = $postRepository->fullPostPage($slug);
33
        if ($item === null) {
34
            return $this->webService->getNotFoundResponse();
35
        }
36
37
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
38
    }
39
40
    public function add(Request $request, FormHydrator $formHydrator): Response
41
    {
42
        $parameters = [
43
            'title' => 'Add post',
44
            'action' => ['blog/add'],
45
            'errors' => [],
46
            'body' => $request->getParsedBody(),
47
        ];
48
49
        if ($request->getMethod() === Method::POST) {
50
            $form = new PostForm();
51
            if ($formHydrator->populateAndValidate($form, $parameters['body'])) {
52
                $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\Blog\Post\PostService::savePost() does only seem to accept App\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

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

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