| Conditions | 11 |
| Paths | 80 |
| Total Lines | 84 |
| Code Lines | 42 |
| 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 |
||
| 22 | public function __invoke( |
||
| 23 | Request $request, |
||
| 24 | CGlossaryRepository $repo, |
||
| 25 | EntityManagerInterface $em, |
||
| 26 | KernelInterface $kernel, |
||
| 27 | TranslatorInterface $translator |
||
| 28 | ): string { |
||
| 29 | $data = json_decode((string) $request->getContent(), true) ?: []; |
||
| 30 | |||
| 31 | $parentResourceNodeId = (int) ($data['parentResourceNodeId'] ?? 0); |
||
| 32 | |||
| 33 | // The frontend may send resourceLinkList as a JSON string or as an array. |
||
| 34 | $resourceLinkListRaw = $data['resourceLinkList'] ?? []; |
||
| 35 | if (is_string($resourceLinkListRaw)) { |
||
| 36 | $resourceLinkListRaw = json_decode($resourceLinkListRaw, true) ?: []; |
||
| 37 | } |
||
| 38 | |||
| 39 | $resourceLinkList = $this->normalizeResourceLinks($resourceLinkListRaw); |
||
| 40 | |||
| 41 | // Resolve context from resource links. |
||
| 42 | $cid = (int) ($resourceLinkList[0]['cid'] ?? 0); |
||
| 43 | $sid = (int) ($resourceLinkList[0]['sid'] ?? 0); |
||
| 44 | |||
| 45 | $course = null; |
||
| 46 | $session = null; |
||
| 47 | |||
| 48 | if ($cid > 0) { |
||
| 49 | $course = $em->getRepository(Course::class)->find($cid); |
||
| 50 | } |
||
| 51 | if ($sid > 0) { |
||
| 52 | $session = $em->getRepository(Session::class)->find($sid); |
||
| 53 | } |
||
| 54 | |||
| 55 | // Important: export only glossary items for the current course/session context. |
||
| 56 | if ($course) { |
||
| 57 | $qb = $repo->getResourcesByCourse($course, $session, null, null, true, true); |
||
| 58 | |||
| 59 | // The alias used by getResourcesByCourse() is "resource" (as seen in GetGlossaryCollectionController). |
||
| 60 | $qb->orderBy('resource.title', 'ASC'); |
||
| 61 | |||
| 62 | /** @var CGlossary[] $glossaryItems */ |
||
| 63 | $glossaryItems = $qb->getQuery()->getResult(); |
||
| 64 | } else { |
||
| 65 | // Fallback to keep behavior resilient if cid is missing. |
||
| 66 | /** @var CGlossary[] $glossaryItems */ |
||
| 67 | $glossaryItems = $repo->findAll(); |
||
| 68 | } |
||
| 69 | |||
| 70 | $exportPath = $kernel->getCacheDir(); |
||
| 71 | $pdfFilePath = $this->generatePdfFile($glossaryItems, $exportPath, $translator); |
||
| 72 | |||
| 73 | if (!empty($pdfFilePath) && file_exists($pdfFilePath)) { |
||
| 74 | $fileName = basename($pdfFilePath); |
||
| 75 | |||
| 76 | // Important: mark as "test" because this file is generated on the server (not uploaded by HTTP). |
||
| 77 | $uploadFile = new UploadedFile( |
||
| 78 | $pdfFilePath, |
||
| 79 | $fileName, |
||
| 80 | 'application/pdf', |
||
| 81 | null, |
||
| 82 | true |
||
| 83 | ); |
||
| 84 | |||
| 85 | $document = new CDocument(); |
||
| 86 | $document->setTitle($fileName); |
||
| 87 | $document->setUploadFile($uploadFile); |
||
| 88 | $document->setFiletype('file'); |
||
| 89 | |||
| 90 | if ($parentResourceNodeId > 0) { |
||
| 91 | $document->setParentResourceNode($parentResourceNodeId); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!empty($resourceLinkList)) { |
||
| 95 | $document->setResourceLinkArray($resourceLinkList); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Save the CDocument entity to the database |
||
| 99 | $em->persist($document); |
||
| 100 | $em->flush(); |
||
| 101 | |||
| 102 | @unlink($pdfFilePath); |
||
|
|
|||
| 103 | } |
||
| 104 | |||
| 105 | return $pdfFilePath; |
||
| 106 | } |
||
| 170 |
If you suppress an error, we recommend checking for the error condition explicitly: