| Conditions | 14 |
| Paths | 58 |
| Total Lines | 119 |
| Code Lines | 79 |
| 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 |
||
| 29 | public function __invoke( |
||
| 30 | Request $request, |
||
| 31 | CDropboxFileRepository $repo, |
||
| 32 | CourseRepository $courseRepo, |
||
| 33 | SessionRepository $sessionRepo, |
||
| 34 | EntityManagerInterface $em, |
||
| 35 | UserHelper $userHelper |
||
| 36 | ): JsonResponse { |
||
| 37 | |||
| 38 | $cid = (int) $request->query->get('cid', 0); |
||
| 39 | $sid = (int) $request->query->get('sid', 0); |
||
| 40 | $gid = (int) $request->query->get('gid', 0); |
||
| 41 | if ($cid <= 0) { |
||
| 42 | throw new BadRequestHttpException('Missing or invalid "cid".'); |
||
| 43 | } |
||
| 44 | $user = $userHelper->getCurrent(); |
||
| 45 | if (!$user) { |
||
| 46 | throw new UnauthorizedHttpException('', 'Unauthorized.'); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** @var UploadedFile|null $file */ |
||
| 50 | $file = $request->files->get('uploadFile'); |
||
| 51 | if (!$file) { |
||
| 52 | // Fallback: accept any uploaded file under a different key and remap it |
||
| 53 | foreach ($request->files as $val) { |
||
| 54 | if ($val instanceof UploadedFile) { |
||
| 55 | $file = $val; |
||
| 56 | $request->files->set('uploadFile', $val); |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } |
||
| 61 | if (!$file) { |
||
| 62 | throw new BadRequestHttpException('"uploadFile" is required'); |
||
| 63 | } |
||
| 64 | |||
| 65 | $parentId = (int) $request->request->get('parentResourceNodeId', 0); |
||
| 66 | if ($parentId === 0) { |
||
| 67 | $course = $courseRepo->find($cid); |
||
| 68 | $parentId = $course?->getResourceNode()?->getId() ?? 0; |
||
| 69 | if ($parentId === 0) { |
||
| 70 | throw new BadRequestHttpException('parentResourceNodeId (categoryId) is required'); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | $description = (string) $request->request->get('description', ''); |
||
| 75 | $categoryId = (int) $request->request->get('categoryId', 0); |
||
| 76 | /** @var string[] $tokens */ |
||
| 77 | $tokens = (array) ($request->request->all('recipients') ?? []); |
||
| 78 | |||
| 79 | // Ensure filename uniqueness |
||
| 80 | $original = $file->getClientOriginalName() ?: 'upload.bin'; |
||
| 81 | $candidate = $original; |
||
| 82 | $i = 1; |
||
| 83 | while ($repo->findOneBy(['filename' => $candidate])) { |
||
| 84 | $pi = pathinfo($original); |
||
| 85 | $name = $pi['filename'] ?? 'file'; |
||
| 86 | $ext = isset($pi['extension']) ? '.'.$pi['extension'] : ''; |
||
| 87 | $candidate = $name.'_'.$i.$ext; |
||
| 88 | $i++; |
||
| 89 | } |
||
| 90 | |||
| 91 | $now = new DateTime(); |
||
| 92 | $e = new CDropboxFile(); |
||
| 93 | $e->setFiletype('file'); |
||
| 94 | $e->setParentResourceNode($parentId); |
||
| 95 | $e->setUploadFile($file); |
||
| 96 | |||
| 97 | $e->setCId($cid); |
||
| 98 | $e->setSessionId($sid); |
||
| 99 | $e->setUploaderId($user->getId()); |
||
| 100 | $e->setTitle($candidate); |
||
| 101 | $e->setFilename($candidate); |
||
| 102 | $e->setFilesize((int) ($file->getSize() ?: 0)); |
||
| 103 | $e->setDescription($description); |
||
| 104 | $e->setAuthor((string) $user->getFullName()); |
||
| 105 | $e->setUploadDate($now); |
||
| 106 | $e->setLastUploadDate($now); |
||
| 107 | $e->setCatId($categoryId); |
||
| 108 | |||
| 109 | // Link visibility (course/session/user/group) |
||
| 110 | $e->setResourceLinkArray($this->buildLinks($tokens, $cid, $sid, $gid)); |
||
| 111 | |||
| 112 | $em->persist($e); |
||
| 113 | $em->flush(); |
||
| 114 | |||
| 115 | // Extract real recipients; if none, leave it as a "Sent only" file (no recipients). |
||
| 116 | $destUserIds = $this->extractUserIdsFromTokens($tokens); |
||
| 117 | |||
| 118 | foreach ($destUserIds as $destUid) { |
||
| 119 | // Create post (feedback/thread seed) |
||
| 120 | $post = new CDropboxPost(); |
||
| 121 | $post->setCId($cid); |
||
| 122 | $post->setSessionId($sid ?? 0); |
||
| 123 | $post->setFileId($e->getIid()); |
||
| 124 | $post->setDestUserId($destUid); |
||
| 125 | $post->setCatId($categoryId); |
||
| 126 | $post->setFeedbackDate(new DateTime()); |
||
| 127 | $em->persist($post); |
||
| 128 | |||
| 129 | // Create visibility for the recipient |
||
| 130 | $p = new CDropboxPerson(); |
||
| 131 | $p->setCId($cid); |
||
| 132 | $p->setUserId($destUid); |
||
| 133 | $p->setFileId($e->getIid()); |
||
| 134 | $em->persist($p); |
||
| 135 | } |
||
| 136 | |||
| 137 | $em->flush(); |
||
| 138 | |||
| 139 | return new JsonResponse([ |
||
| 140 | 'ok' => true, |
||
| 141 | 'iid' => (int) $e->getIid(), |
||
| 142 | 'title' => $e->getTitle(), |
||
| 143 | 'filename' => $e->getFilename(), |
||
| 144 | 'filesize' => $e->getFilesize(), |
||
| 145 | 'categoryId' => $e->getCatId(), |
||
| 146 | 'message' => 'File uploaded successfully', |
||
| 147 | ], 201); |
||
| 148 | } |
||
| 211 |