Conditions | 9 |
Paths | 47 |
Total Lines | 73 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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(); |
||
194 |