Passed
Pull Request — master (#138)
by Alexander
38:48 queued 23:47
created

PostController::index()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 12
rs 10
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\ViewRenderer;
10
use App\Service\WebControllerService;
11
use App\Blog\Entity\Tag;
12
use App\Entity\User;
13
use Cycle\ORM\ORMInterface;
14
use Cycle\ORM\Transaction;
15
use Psr\Http\Message\ResponseFactoryInterface;
16
use Psr\Http\Message\ResponseInterface as Response;
17
use Psr\Http\Message\ServerRequestInterface as Request;
18
use Yiisoft\Http\Method;
19
use Yiisoft\Router\UrlGeneratorInterface;
20
use Yiisoft\Yii\View\ViewRenderer;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use Yiisoft\Yii\View\ViewRenderer as ViewRenderer because the name is already in use
Loading history...
21
use Yiisoft\Yii\Web\User\User as UserComponent;
22
23
final class PostController
24
{
25
    private ViewRenderer $viewRenderer;
26
    private WebControllerService $webService;
27
    private PostService $postService;
28
    private UserService $userService;
29
30
    public function __construct(
31
        ViewRenderer $viewRenderer,
32
        WebControllerService $webService,
33
        PostService $postService,
34
        UserService $userService
35
    ) {
36
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
37
        $this->webService = $webService;
38
        $this->postService = $postService;
39
        $this->userService = $userService;
40
    }
41
42
    public function index(Request $request, PostRepository $postRepository): Response
43
    {
44
        $canEdit = $this->userService->hasPermission('editPost');
45
        $slug = $request->getAttribute('slug', null);
46
        $item = $postRepository->fullPostPage($slug);
47
        if ($item === null) {
48
            return $this->webService->getNotFoundResponse();
49
        }
50
51
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
52
    }
53
54
    public function add(Request $request, PostForm $form): Response
55
    {
56
        $parameters = [
57
            'title' => 'Add post',
58
            'action' => ['blog/add'],
59
            'errors' => [],
60
            'body' => $request->getParsedBody(),
61
        ];
62
63
        if ($request->getMethod() === Method::POST) {
64
            $form->load($parameters['body']);
65
            if ($form->validate()) {
66
                $this->postService->savePost($this->userService->getUser(), new Post(), $form);
67
                return $this->webService->getRedirectResponse('blog/index');
68
            }
69
70
            $parameters['errors'] = $form->firstErrors();
71
        }
72
73
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
74
    }
75
76
    public function edit(Request $request, PostForm $form, PostRepository $postRepository): Response
77
    {
78
        $slug = $request->getAttribute('slug', null);
79
        $post = $postRepository->fullPostPage($slug);
80
        if ($post === null) {
81
            return $this->webService->getNotFoundResponse();
82
        }
83
84
        $parameters = [
85
            'title' => 'Edit post',
86
            'action' => ['blog/edit', ['slug' => $slug]],
87
            'errors' => [],
88
            'body' => [
89
                'title' => $post->getTitle(),
90
                'content' => $post->getContent(),
91
                'tags' => $this->postService->getPostTags($post)
92
            ]
93
        ];
94
95
        if ($request->getMethod() === Method::POST) {
96
            $body = $request->getParsedBody();
97
            $form->load($body);
98
            if ($form->validate()) {
99
                $this->postService->savePost($this->userService->getUser(), $post, $form);
100
                return $this->webService->getRedirectResponse('blog/index');
101
            }
102
103
            $parameters['body'] = $body;
104
            $parameters['errors'] = $form->firstErrors();
105
        }
106
107
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
108
    }
109
}
110