| Conditions | 19 |
| Paths | 37 |
| Total Lines | 98 |
| Code Lines | 50 |
| 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 |
||
| 30 | public static function setLinks(AbstractResource $resource, EntityManagerInterface $em): void |
||
| 31 | { |
||
| 32 | $resourceNode = $resource->getResourceNode(); |
||
| 33 | $links = $resource->getResourceLinkArray(); |
||
| 34 | if ($links) { |
||
|
|
|||
| 35 | $groupRepo = $em->getRepository(CGroup::class); |
||
| 36 | $courseRepo = $em->getRepository(Course::class); |
||
| 37 | $sessionRepo = $em->getRepository(Session::class); |
||
| 38 | $userRepo = $em->getRepository(User::class); |
||
| 39 | |||
| 40 | foreach ($links as $link) { |
||
| 41 | $resourceLink = new ResourceLink(); |
||
| 42 | $linkSet = false; |
||
| 43 | if (isset($link['cid']) && !empty($link['cid'])) { |
||
| 44 | $course = $courseRepo->find($link['cid']); |
||
| 45 | if (null !== $course) { |
||
| 46 | $linkSet = true; |
||
| 47 | $resourceLink->setCourse($course); |
||
| 48 | } else { |
||
| 49 | throw new InvalidArgumentException(sprintf('Course #%s does not exists', $link['cid'])); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | if (isset($link['sid']) && !empty($link['sid'])) { |
||
| 54 | $session = $sessionRepo->find($link['sid']); |
||
| 55 | if (null !== $session) { |
||
| 56 | $linkSet = true; |
||
| 57 | $resourceLink->setSession($session); |
||
| 58 | } else { |
||
| 59 | throw new InvalidArgumentException(sprintf('Session #%s does not exists', $link['sid'])); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | if (isset($link['gid']) && !empty($link['gid'])) { |
||
| 64 | $group = $groupRepo->find($link['gid']); |
||
| 65 | if (null !== $group) { |
||
| 66 | $linkSet = true; |
||
| 67 | $resourceLink->setGroup($group); |
||
| 68 | } else { |
||
| 69 | throw new InvalidArgumentException(sprintf('Group #%s does not exists', $link['gid'])); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | if (isset($link['uid']) && !empty($link['uid'])) { |
||
| 74 | $user = $userRepo->find($link['uid']); |
||
| 75 | if (null !== $user) { |
||
| 76 | $linkSet = true; |
||
| 77 | $resourceLink->setUser($user); |
||
| 78 | } else { |
||
| 79 | throw new InvalidArgumentException(sprintf('User #%s does not exists', $link['uid'])); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | if (isset($link['visibility'])) { |
||
| 84 | $resourceLink->setVisibility((int) $link['visibility']); |
||
| 85 | } else { |
||
| 86 | throw new InvalidArgumentException('Link needs a visibility key'); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($linkSet) { |
||
| 90 | $em->persist($resourceLink); |
||
| 91 | $resourceNode->addResourceLink($resourceLink); |
||
| 92 | // $em->persist($resourceNode); |
||
| 93 | // $em->persist($resource->getResourceNode()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | // Use by Chamilo not api platform. |
||
| 99 | $links = $resource->getResourceLinkEntityList(); |
||
| 100 | if ($links) { |
||
| 101 | // error_log('$resource->getResourceLinkEntityList()'); |
||
| 102 | foreach ($links as $link) { |
||
| 103 | /*$rights = []; |
||
| 104 | switch ($link->getVisibility()) { |
||
| 105 | case ResourceLink::VISIBILITY_PENDING: |
||
| 106 | case ResourceLink::VISIBILITY_DRAFT: |
||
| 107 | $editorMask = ResourceNodeVoter::getEditorMask(); |
||
| 108 | $resourceRight = new ResourceRight(); |
||
| 109 | $resourceRight |
||
| 110 | ->setMask($editorMask) |
||
| 111 | ->setRole(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER) |
||
| 112 | ; |
||
| 113 | $rights[] = $resourceRight; |
||
| 114 | |||
| 115 | break; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!empty($rights)) { |
||
| 119 | foreach ($rights as $right) { |
||
| 120 | $link->addResourceRight($right); |
||
| 121 | } |
||
| 122 | }*/ |
||
| 123 | // error_log('link adding to node: '.$resource->getResourceNode()->getId()); |
||
| 124 | // error_log('link with user : '.$link->getUser()->getUsername()); |
||
| 125 | $resource->getResourceNode()->addResourceLink($link); |
||
| 126 | |||
| 127 | $em->persist($link); |
||
| 128 | } |
||
| 341 |
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.