| Conditions | 16 |
| Paths | 154 |
| Total Lines | 132 |
| Code Lines | 87 |
| 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 | // --- Contexto |
||
| 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 | // --- Normaliza: queremos SIEMPRE uploadFile |
||
| 50 | /** @var UploadedFile|null $file */ |
||
| 51 | $file = $request->files->get('uploadFile'); |
||
| 52 | if (!$file) { |
||
| 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 | // --- Nodo padre: si no viene, usamos el del curso |
||
| 66 | $parentId = (int) $request->request->get('parentResourceNodeId', 0); |
||
| 67 | if ($parentId === 0) { |
||
| 68 | $course = $courseRepo->find($cid); |
||
| 69 | $parentId = $course?->getResourceNode()?->getId() ?? 0; |
||
| 70 | if ($parentId === 0) { |
||
| 71 | throw new BadRequestHttpException('parentResourceNodeId (categoryId) is required'); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | $description = (string) $request->request->get('description', ''); |
||
| 76 | $categoryId = (int) $request->request->get('categoryId', 0); |
||
| 77 | /** @var string[] $tokens */ |
||
| 78 | $tokens = (array) ($request->request->all('recipients') ?? []); |
||
| 79 | |||
| 80 | // --- Asegura filename único (UNIQUE en filename) |
||
| 81 | $original = $file->getClientOriginalName() ?: 'upload.bin'; |
||
| 82 | $candidate = $original; |
||
| 83 | $i = 1; |
||
| 84 | while ($repo->findOneBy(['filename' => $candidate])) { |
||
| 85 | $pi = pathinfo($original); |
||
| 86 | $name = $pi['filename'] ?? 'file'; |
||
| 87 | $ext = isset($pi['extension']) ? '.'.$pi['extension'] : ''; |
||
| 88 | $candidate = $name.'_'.$i.$ext; |
||
| 89 | $i++; |
||
| 90 | } |
||
| 91 | |||
| 92 | // --- Construye el recurso tipo archivo (activa ResourceListener) |
||
| 93 | $now = new DateTime(); |
||
| 94 | $e = new CDropboxFile(); |
||
| 95 | $e->setFiletype('file'); // importante |
||
| 96 | $e->setParentResourceNode($parentId); // importante |
||
| 97 | $e->setUploadFile($file); // importante |
||
| 98 | |||
| 99 | $e->setCId($cid); |
||
| 100 | $e->setSessionId($sid); |
||
| 101 | $e->setUploaderId($user->getId()); |
||
| 102 | $e->setTitle($candidate); |
||
| 103 | $e->setFilename($candidate); |
||
| 104 | $e->setFilesize((int) ($file->getSize() ?: 0)); |
||
| 105 | $e->setDescription($description); |
||
| 106 | $e->setAuthor((string) $user->getFullName()); |
||
| 107 | $e->setUploadDate($now); |
||
| 108 | $e->setLastUploadDate($now); |
||
| 109 | $e->setCatId($categoryId); |
||
| 110 | |||
| 111 | // Links de visibilidad (ResourceLinkArray) |
||
| 112 | $e->setResourceLinkArray($this->buildLinks($tokens, $cid, $sid, $gid)); |
||
| 113 | |||
| 114 | // --- Guarda recurso para tener iid |
||
| 115 | $em->persist($e); |
||
| 116 | $em->flush(); |
||
| 117 | |||
| 118 | // -------- Legacy: Persons + Posts ---------- |
||
| 119 | // Persona para uploader |
||
| 120 | $up = new CDropboxPerson(); |
||
| 121 | $up->setCId($cid); |
||
| 122 | $up->setUserId($user->getId()); |
||
| 123 | $up->setFileId($e->getIid()); |
||
| 124 | $em->persist($up); |
||
| 125 | |||
| 126 | $destUserIds = $this->extractUserIdsFromTokens($tokens, $user->getId()); |
||
| 127 | if (!$destUserIds) { |
||
|
|
|||
| 128 | $destUserIds = [$user->getId()]; |
||
| 129 | } |
||
| 130 | |||
| 131 | foreach ($destUserIds as $destUid) { |
||
| 132 | $post = new CDropboxPost(); |
||
| 133 | $post->setCId($cid); |
||
| 134 | $post->setSessionId($sid ?? 0); |
||
| 135 | $post->setFileId($e->getIid()); |
||
| 136 | $post->setDestUserId($destUid); |
||
| 137 | $post->setCatId($categoryId); |
||
| 138 | $post->setFeedbackDate(new DateTime()); |
||
| 139 | $em->persist($post); |
||
| 140 | |||
| 141 | if ($destUid !== $user->getId()) { |
||
| 142 | $p = new CDropboxPerson(); |
||
| 143 | $p->setCId($cid); |
||
| 144 | $p->setUserId($destUid); |
||
| 145 | $p->setFileId($e->getIid()); |
||
| 146 | $em->persist($p); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | $em->flush(); |
||
| 151 | |||
| 152 | return new JsonResponse([ |
||
| 153 | 'ok' => true, |
||
| 154 | 'iid' => (int) $e->getIid(), |
||
| 155 | 'title' => $e->getTitle(), |
||
| 156 | 'filename' => $e->getFilename(), |
||
| 157 | 'filesize' => $e->getFilesize(), |
||
| 158 | 'categoryId' => $e->getCatId(), |
||
| 159 | 'message' => 'File uploaded successfully', |
||
| 160 | ], 201); |
||
| 161 | } |
||
| 218 |
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.