Passed
Pull Request — master (#122)
by Sergei
12:04
created

PostController::edit()   B

Complexity

Conditions 9
Paths 47

Size

Total Lines 73
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 45
nc 47
nop 5
dl 0
loc 73
rs 7.6444
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Post;
6
7
use App\ViewRenderer\ViewRenderer;
8
use App\Blog\Entity\Post;
9
use App\Blog\Entity\Tag;
10
use App\Entity\User;
11
use App\ViewRenderer;
0 ignored issues
show
Bug introduced by
A parse error occurred: Cannot use App\ViewRenderer as ViewRenderer because the name is already in use
Loading history...
12
use Cycle\ORM\ORMInterface;
13
use Cycle\ORM\Transaction;
14
use Psr\Http\Message\ResponseFactoryInterface;
15
use Psr\Http\Message\ResponseInterface as Response;
16
use Psr\Http\Message\ServerRequestInterface as Request;
17
use Psr\Log\LoggerInterface;
18
use Yiisoft\Access\AccessCheckerInterface;
19
use Yiisoft\Http\Method;
20
use Yiisoft\Router\UrlGeneratorInterface;
21
use Yiisoft\Yii\Web\User\User as UserComponent;
22
23
final class PostController
24
{
25
    private ViewRenderer $viewRenderer;
26
    private ResponseFactoryInterface $responseFactory;
27
    private LoggerInterface $logger;
28
    private UserComponent $userService;
29
30
    public function __construct(
31
        ViewRenderer $viewRenderer,
32
        ResponseFactoryInterface $responseFactory,
33
        LoggerInterface $logger,
34
        UserComponent $userService
35
    ) {
36
        $this->viewRenderer = $viewRenderer->withControllerName('blog/post');
37
        $this->responseFactory = $responseFactory;
38
        $this->logger = $logger;
39
        $this->userService = $userService;
40
    }
41
42
    public function index(Request $request, PostRepository $postRepository, AccessCheckerInterface $accessChecker): Response
43
    {
44
        $userId = $this->userService->getId();
45
        $canEdit = !is_null($userId) && $accessChecker->userHasPermission($userId, 'editPost');
46
47
        $slug = $request->getAttribute('slug', null);
48
        $item = $postRepository->fullPostPage($slug);
49
        if ($item === null) {
50
            return $this->responseFactory->createResponse(404);
51
        }
52
53
        return $this->viewRenderer->render('index', ['item' => $item, 'canEdit' => $canEdit, 'slug' => $slug]);
54
    }
55
56
    public function add(
57
        Request $request,
58
        ORMInterface $orm,
59
        UrlGeneratorInterface $urlGenerator
60
    ): Response {
61
        if ($this->userService->isGuest()) {
62
            return $this->responseFactory->createResponse(403);
63
        }
64
65
        $body = $request->getParsedBody();
66
        $parameters = [
67
            'body' => $body,
68
            'action' => ['blog/add']
69
        ];
70
71
        if ($request->getMethod() === Method::POST) {
72
            $error = '';
73
74
            try {
75
                foreach (['title', 'content'] as $name) {
76
                    if (empty($body[$name])) {
77
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required');
78
                    }
79
                }
80
81
                $post = new Post($body['title'], $body['content']);
82
83
                $userRepo = $orm->getRepository(User::class);
84
                $user = $userRepo->findByPK($this->userService->getId());
85
86
                $post->setUser($user);
87
                $post->setPublic(true);
88
89
                $tagRepository = $orm->getRepository(Tag::class);
90
                foreach ($body['tags'] ?? [] as $tag) {
91
                    $tagEntity = $tagRepository->getOrCreate($tag);
92
                    $post->addTag($tagEntity);
93
                }
94
95
                $transaction = new Transaction($orm);
96
                $transaction->persist($post);
97
98
                $transaction->run();
99
100
                return $this->responseFactory
101
                    ->createResponse(302)
102
                    ->withHeader(
103
                        'Location',
104
                        $urlGenerator->generate('blog/index')
105
                    );
106
            } catch (\Throwable $e) {
107
                $this->logger->error($e);
108
                $error = $e->getMessage();
109
            }
110
111
            $parameters['error'] = $error;
112
        }
113
114
        $parameters['title'] = 'Add post';
115
        $parameters['tags'] = [];
116
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
117
    }
118
119
    public function edit(
120
        Request $request,
121
        ORMInterface $orm,
122
        UrlGeneratorInterface $urlGenerator,
123
        PostRepository $postRepository,
124
        AccessCheckerInterface $accessChecker
125
    ): Response {
126
        $userId = $this->userService->getId();
127
        if (is_null($userId) || !$accessChecker->userHasPermission($userId, 'editPost')) {
128
            return $this->responseFactory->createResponse(403);
129
        }
130
131
        $slug = $request->getAttribute('slug', null);
132
        $post = $postRepository->fullPostPage($slug);
133
        if ($post === null) {
134
            return $this->responseFactory->createResponse(404);
135
        }
136
137
        $parameters = [];
138
        $parameters['action'] = ['blog/edit', ['slug' => $slug]];
139
140
        if ($request->getMethod() === Method::POST) {
141
            $error = '';
142
143
            try {
144
                $body = $request->getParsedBody();
145
                $parameters['body'] = $body;
146
147
                foreach (['title', 'content'] as $name) {
148
                    if (empty($body[$name])) {
149
                        throw new \InvalidArgumentException(ucfirst($name) . ' is required');
150
                    }
151
                }
152
153
                $post->setTitle($body['title']);
154
                $post->setContent($body['content']);
155
156
                $tagRepository = $orm->getRepository(Tag::class);
157
                $post->resetTags();
158
                foreach ($body['tags'] ?? [] as $tag) {
159
                    $tagEntity = $tagRepository->getOrCreate($tag);
160
                    $post->addTag($tagEntity);
161
                }
162
163
                $transaction = new Transaction($orm);
164
                $transaction->persist($post);
165
166
                $transaction->run();
167
168
                return $this->responseFactory
169
                    ->createResponse(302)
170
                    ->withHeader(
171
                        'Location',
172
                        $urlGenerator->generate('blog/index')
173
                    );
174
            } catch (\Throwable $e) {
175
                $this->logger->error($e);
176
                $error = $e->getMessage();
177
            }
178
179
            $parameters['error'] = $error;
180
        } else {
181
            $parameters['body'] = [
182
                'title' => $post->getTitle(),
183
                'content' => $post->getContent(),
184
            ];
185
        }
186
187
        $parameters['title'] = 'Edit post';
188
        $parameters['tags'] = array_map(function (Tag $tag) {
189
            return $tag->getLabel();
190
        }, $post->getTags());
191
        return $this->viewRenderer->withCsrf()->render('__form', $parameters);
192
    }
193
}
194