| Conditions | 25 |
| Paths | 28 |
| Total Lines | 95 |
| Code Lines | 53 |
| 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 |
||
| 159 | private function readCertificateHtml(GradebookCertificate $certificate, string $hash): string |
||
| 160 | { |
||
| 161 | // Preferred flow: read from ResourceNode |
||
| 162 | if ($certificate->hasResourceNode()) { |
||
| 163 | $node = $certificate->getResourceNode(); |
||
| 164 | $fs = $this->resourceNodeRepository->getFileSystem(); |
||
| 165 | |||
| 166 | if ($fs) { |
||
|
|
|||
| 167 | $basePath = rtrim((string) $node->getPath(), '/'); |
||
| 168 | |||
| 169 | // Helper to create sharded path: resource/7/4/3/<filename> |
||
| 170 | $sharded = static function (string $filename): string { |
||
| 171 | $a = $filename[0] ?? '_'; |
||
| 172 | $b = $filename[1] ?? '_'; |
||
| 173 | $c = $filename[2] ?? '_'; |
||
| 174 | |||
| 175 | return sprintf('resource/%s/%s/%s/%s', $a, $b, $c, $filename); |
||
| 176 | }; |
||
| 177 | |||
| 178 | // Try via ResourceFile->title first (this is usually the stored physical filename) |
||
| 179 | foreach ($node->getResourceFiles() as $rf) { |
||
| 180 | $title = (string) $rf->getTitle(); |
||
| 181 | if ($title !== '') { |
||
| 182 | if ($basePath !== '') { |
||
| 183 | $p = $basePath.'/'.$title; |
||
| 184 | if ($fs->fileExists($p)) { |
||
| 185 | $content = $fs->read($p); |
||
| 186 | if ($content !== false && $content !== null) { |
||
| 187 | return $content; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | $p2 = $sharded($title); |
||
| 193 | if ($fs->fileExists($p2)) { |
||
| 194 | $content = $fs->read($p2); |
||
| 195 | if ($content !== false && $content !== null) { |
||
| 196 | return $content; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | // Try via ResourceFile->original_name |
||
| 203 | foreach ($node->getResourceFiles() as $rf) { |
||
| 204 | $orig = (string) $rf->getOriginalName(); |
||
| 205 | if ($orig !== '') { |
||
| 206 | if ($basePath !== '') { |
||
| 207 | $p = $basePath.'/'.$orig; |
||
| 208 | if ($fs->fileExists($p)) { |
||
| 209 | $content = $fs->read($p); |
||
| 210 | if ($content !== false && $content !== null) { |
||
| 211 | return $content; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | $p2 = $sharded($orig); |
||
| 217 | if ($fs->fileExists($p2)) { |
||
| 218 | $content = $fs->read($p2); |
||
| 219 | if ($content !== false && $content !== null) { |
||
| 220 | return $content; |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | // Final resource fallback (may still fail if no default file is set) |
||
| 228 | try { |
||
| 229 | return $this->resourceNodeRepository->getResourceNodeFileContent($node); |
||
| 230 | } catch (\Throwable $e) { |
||
| 231 | // Continue to legacy fallback |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | // Legacy flow: PersonalFile by title |
||
| 236 | $filename = $hash.'.html'; |
||
| 237 | $candidates = [$filename, '/'.$filename, $hash, '/'.$hash]; |
||
| 238 | |||
| 239 | $personalFileRepo = Container::getPersonalFileRepository(); |
||
| 240 | $pf = null; |
||
| 241 | foreach ($candidates as $cand) { |
||
| 242 | $row = $personalFileRepo->findOneBy(['title' => $cand]); |
||
| 243 | if ($row) { |
||
| 244 | $pf = $row; |
||
| 245 | break; |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | if (!$pf) { |
||
| 250 | throw new NotFoundHttpException('The certificate file was not found.'); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $personalFileRepo->getResourceFileContent($pf); |
||
| 254 | } |
||
| 256 |