Conditions | 11 |
Paths | 60 |
Total Lines | 133 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
229 | protected function handleUpdateRequest(AbstractResource $resource, ResourceRepository $repo, Request $request, EntityManager $em) |
||
230 | { |
||
231 | error_log('handleUpdateRequest'); |
||
232 | $contentData = $request->getContent(); |
||
233 | $resourceLinkList = []; |
||
234 | if (!empty($contentData)) { |
||
235 | error_log('contentData'); |
||
236 | $contentData = json_decode($contentData, true); |
||
237 | $title = $contentData['title'] ?? ''; |
||
238 | $content = $contentData['contentFile'] ?? ''; |
||
239 | $resourceLinkList = $contentData['resourceLinkListFromEntity'] ?? []; |
||
240 | } else { |
||
241 | error_log('else'); |
||
242 | $title = $request->get('title'); |
||
243 | $content = $request->request->get('contentFile'); |
||
244 | //$comment = $request->request->get('comment'); |
||
245 | } |
||
246 | |||
247 | $repo->setResourceName($resource, $title); |
||
248 | |||
249 | $hasFile = $resource->getResourceNode()->hasResourceFile(); |
||
250 | |||
251 | $resourceNode = $resource->getResourceNode(); |
||
252 | |||
253 | if ($hasFile && !empty($content)) { |
||
254 | if ($resourceNode->hasResourceFile()) { |
||
255 | // The content is updated by the ResourceNodeListener.php |
||
256 | $resourceNode->setContent($content); |
||
257 | $resourceNode->getResourceFile()->setSize(\strlen($content)); |
||
258 | } |
||
259 | $resourceNode->getResourceFile()->setUpdatedAt(new DateTime()); |
||
260 | $resource->setResourceNode($resourceNode); |
||
261 | } |
||
262 | |||
263 | $link = null; |
||
264 | if (!empty($resourceLinkList)) { |
||
265 | foreach ($resourceLinkList as $linkArray) { |
||
266 | // Find the exact link. |
||
267 | $linkId = $linkArray['id'] ?? 0; |
||
268 | if (!empty($linkId)) { |
||
269 | /** @var ResourceLink $link */ |
||
270 | $link = $resourceNode->getResourceLinks()->filter(fn ($link) => $link->getId() === $linkId)->first(); |
||
271 | |||
272 | if (null !== $link) { |
||
273 | $link->setVisibility((int) $linkArray['visibility']); |
||
274 | |||
275 | break; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | error_log(print_r($resourceLinkList, true)); |
||
281 | $resource->setResourceLinkArray($resourceLinkList); |
||
282 | $this->setLinks($resource, $em); |
||
283 | /* |
||
284 | |||
285 | $groupRepo = $em->getRepository(CGroup::class); |
||
286 | $courseRepo = $em->getRepository(Course::class); |
||
287 | $sessionRepo = $em->getRepository(Session::class); |
||
288 | $userRepo = $em->getRepository(User::class); |
||
289 | |||
290 | foreach ($resourceLinkList as $link) { |
||
291 | $resourceLink = new ResourceLink(); |
||
292 | $linkSet = false; |
||
293 | if (isset($link['cid']) && !empty($link['cid'])) { |
||
294 | $course = $courseRepo->find($link['cid']); |
||
295 | if (null !== $course) { |
||
296 | $linkSet = true; |
||
297 | $resourceLink->setCourse($course); |
||
298 | } else { |
||
299 | throw new InvalidArgumentException(sprintf('Course #%s does not exists', $link['cid'])); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | if (isset($link['sid']) && !empty($link['sid'])) { |
||
304 | $session = $sessionRepo->find($link['sid']); |
||
305 | if (null !== $session) { |
||
306 | $linkSet = true; |
||
307 | $resourceLink->setSession($session); |
||
308 | } else { |
||
309 | throw new InvalidArgumentException(sprintf('Session #%s does not exists', $link['sid'])); |
||
310 | } |
||
311 | } |
||
312 | |||
313 | if (isset($link['gid']) && !empty($link['gid'])) { |
||
314 | $group = $groupRepo->find($link['gid']); |
||
315 | if (null !== $group) { |
||
316 | $linkSet = true; |
||
317 | $resourceLink->setGroup($group); |
||
318 | } else { |
||
319 | throw new InvalidArgumentException(sprintf('Group #%s does not exists', $link['gid'])); |
||
320 | } |
||
321 | } |
||
322 | |||
323 | if (isset($link['uid']) && !empty($link['uid'])) { |
||
324 | $user = $userRepo->find($link['uid']); |
||
325 | if (null !== $user) { |
||
326 | $linkSet = true; |
||
327 | $resourceLink->setUser($user); |
||
328 | } else { |
||
329 | throw new InvalidArgumentException(sprintf('User #%s does not exists', $link['uid'])); |
||
330 | } |
||
331 | } |
||
332 | |||
333 | if (isset($link['visibility'])) { |
||
334 | $resourceLink->setVisibility((int) $link['visibility']); |
||
335 | } else { |
||
336 | throw new InvalidArgumentException('Link needs a visibility key'); |
||
337 | } |
||
338 | |||
339 | if ($linkSet) { |
||
340 | error_log('link added'); |
||
341 | $em->persist($resourceLink); |
||
342 | //$resourceLink->setResourceNode($resource->getResourceNode()); |
||
343 | $resource->getResourceNode()->addResourceLink($resourceLink); |
||
344 | $em->persist($resource->getResourceNode()); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | */ |
||
349 | } |
||
350 | |||
351 | $isRecursive = !$hasFile; |
||
352 | // If it's a folder then change the visibility to the children (That have the same link). |
||
353 | if ($isRecursive && null !== $link) { |
||
354 | $repo->copyVisibilityToChildren($resource->getResourceNode(), $link); |
||
355 | } |
||
356 | |||
357 | $resourceNode->setUpdatedAt(new DateTime()); |
||
358 | |||
359 | error_log('Finish update resource node file action'); |
||
360 | |||
361 | return $resource; |
||
362 | } |
||
364 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.