| Conditions | 13 |
| Paths | 154 |
| Total Lines | 108 |
| Code Lines | 66 |
| 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 |
||
| 23 | #[Route('/save', name: 'chamilo_core_legal_save', methods: ['POST'])] |
||
| 24 | public function saveLegal( |
||
| 25 | Request $request, |
||
| 26 | EntityManagerInterface $entityManager, |
||
| 27 | LegalRepository $legalRepository |
||
| 28 | ): Response { |
||
| 29 | $data = json_decode($request->getContent(), true); |
||
| 30 | |||
| 31 | $languageId = (int) ($data['lang'] ?? 0); |
||
| 32 | if ($languageId <= 0) { |
||
| 33 | return new JsonResponse(['message' => 'Invalid language id'], Response::HTTP_BAD_REQUEST); |
||
| 34 | } |
||
| 35 | |||
| 36 | $changes = (string) ($data['changes'] ?? ''); |
||
| 37 | $sections = $data['sections'] ?? null; |
||
| 38 | |||
| 39 | if (!is_array($sections)) { |
||
| 40 | return new JsonResponse(['message' => 'Missing sections payload'], Response::HTTP_BAD_REQUEST); |
||
| 41 | } |
||
| 42 | |||
| 43 | // Normalize & hash incoming payload (idempotency). |
||
| 44 | $normalize = static function ($v): string { |
||
| 45 | $s = is_string($v) ? $v : ''; |
||
| 46 | $s = str_replace(["\r\n", "\r"], "\n", $s); |
||
| 47 | $s = trim($s); |
||
| 48 | |||
| 49 | return $s; |
||
| 50 | }; |
||
| 51 | |||
| 52 | $incoming = []; |
||
| 53 | for ($type = 0; $type <= 15; $type++) { |
||
| 54 | $key = (string) $type; |
||
| 55 | $incoming[$type] = $normalize($sections[$key] ?? ($sections[$type] ?? '')); |
||
| 56 | } |
||
| 57 | $incomingHash = hash('sha256', json_encode($incoming, JSON_UNESCAPED_UNICODE)); |
||
| 58 | |||
| 59 | $conn = $entityManager->getConnection(); |
||
| 60 | $conn->beginTransaction(); |
||
| 61 | |||
| 62 | try { |
||
| 63 | // Generic concurrency control: lock the language row. |
||
| 64 | // This prevents two concurrent saves for the same language. |
||
| 65 | $conn->executeStatement( |
||
| 66 | 'SELECT id FROM language WHERE id = :id FOR UPDATE', |
||
| 67 | ['id' => $languageId] |
||
| 68 | ); |
||
| 69 | |||
| 70 | // Get latest version (inside the lock). |
||
| 71 | $lastVersion = (int) $legalRepository->findLatestVersionByLanguage($languageId); |
||
| 72 | |||
| 73 | if ($lastVersion > 0) { |
||
| 74 | // Load last saved sections to compare. |
||
| 75 | $rows = $conn->fetchAllAssociative( |
||
| 76 | 'SELECT type, content |
||
| 77 | FROM legal |
||
| 78 | WHERE language_id = :lang AND version = :ver |
||
| 79 | ORDER BY type ASC', |
||
| 80 | ['lang' => $languageId, 'ver' => $lastVersion] |
||
| 81 | ); |
||
| 82 | |||
| 83 | $existing = array_fill(0, 16, ''); |
||
| 84 | foreach ($rows as $r) { |
||
| 85 | $t = (int) ($r['type'] ?? -1); |
||
| 86 | if ($t >= 0 && $t <= 15) { |
||
| 87 | $existing[$t] = $normalize($r['content'] ?? ''); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | $existingHash = hash('sha256', json_encode($existing, JSON_UNESCAPED_UNICODE)); |
||
| 91 | |||
| 92 | // No changes => do NOT create a new version. |
||
| 93 | if ($existingHash === $incomingHash) { |
||
| 94 | $conn->commit(); |
||
| 95 | |||
| 96 | return new JsonResponse([ |
||
| 97 | 'message' => 'No changes detected', |
||
| 98 | 'version' => $lastVersion, |
||
| 99 | ], Response::HTTP_OK); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | // Create new version only when changed. |
||
| 104 | $newVersion = $lastVersion + 1; |
||
| 105 | $timestamp = time(); |
||
| 106 | |||
| 107 | for ($type = 0; $type <= 15; $type++) { |
||
| 108 | $content = $incoming[$type]; |
||
| 109 | |||
| 110 | $legal = new Legal(); |
||
| 111 | $legal->setLanguageId($languageId); |
||
| 112 | $legal->setVersion($newVersion); |
||
| 113 | $legal->setType($type); |
||
| 114 | $legal->setDate($timestamp); |
||
| 115 | $legal->setChanges($changes); |
||
| 116 | $legal->setContent($content === '' ? null : $content); |
||
| 117 | |||
| 118 | $entityManager->persist($legal); |
||
| 119 | } |
||
| 120 | |||
| 121 | $entityManager->flush(); |
||
| 122 | $conn->commit(); |
||
| 123 | |||
| 124 | return new JsonResponse([ |
||
| 125 | 'message' => 'Terms saved successfully', |
||
| 126 | 'version' => $newVersion, |
||
| 127 | ], Response::HTTP_OK); |
||
| 128 | } catch (\Throwable $e) { |
||
| 129 | $conn->rollBack(); |
||
| 130 | throw $e; |
||
| 131 | } |
||
| 205 |
This check looks for private methods that have been defined, but are not used inside the class.