| Conditions | 6 |
| Paths | 12 |
| Total Lines | 64 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 23 | public function insertInvalidationEvent( |
||
| 24 | string $type, |
||
| 25 | string $identifier, |
||
| 26 | ?string $connection_name = null, |
||
| 27 | ?string $reason = null, |
||
| 28 | ?int $totalShards = 0, |
||
| 29 | ?int $priority = 0, |
||
| 30 | ?array $associatedIdentifiers = [], |
||
|
|
|||
| 31 | ): void { |
||
| 32 | $shard = crc32($identifier) % ($totalShards > 0 ? $totalShards : config('super_cache_invalidate.total_shards', 10)); |
||
| 33 | |||
| 34 | $redisConnectionName = $connection_name ?? config('super_cache_invalidate.default_connection_name'); |
||
| 35 | $data = [ |
||
| 36 | 'type' => $type, |
||
| 37 | 'identifier' => $identifier, |
||
| 38 | 'connection_name' => $redisConnectionName, |
||
| 39 | 'reason' => $reason, |
||
| 40 | 'priority' => $priority, |
||
| 41 | 'event_time' => now(), |
||
| 42 | 'processed' => 0, |
||
| 43 | 'shard' => $shard, |
||
| 44 | ]; |
||
| 45 | |||
| 46 | $maxAttempts = 5; |
||
| 47 | $attempts = 0; |
||
| 48 | $insertOk = false; |
||
| 49 | |||
| 50 | while ($attempts < $maxAttempts && !$insertOk) { |
||
| 51 | //DB::beginTransaction(); |
||
| 52 | |||
| 53 | try { |
||
| 54 | // Cerca di bloccare il record per l'inserimento |
||
| 55 | $partitionCache_invalidation_events = $this->getCacheInvalidationEventsPartitionName($shard, $priority); |
||
| 56 | |||
| 57 | //$eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data); |
||
| 58 | DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insert($data); |
||
| 59 | // Insert associated identifiers |
||
| 60 | // TODO JB 31/12/2024: per adesso commentato, da riattivare quando tutto funziona alla perfezione usando la partizione, |
||
| 61 | // anche perchè la chiave primaria è data da id e partiton_key, per cui va capito cosa restituisce insertGetId e se è bloccante |
||
| 62 | /* |
||
| 63 | if (!empty($associatedIdentifiers)) { |
||
| 64 | $associations = []; |
||
| 65 | foreach ($associatedIdentifiers as $associated) { |
||
| 66 | $associations[] = [ |
||
| 67 | 'event_id' => $eventId, |
||
| 68 | 'associated_type' => $associated['type'], // 'key' or 'tag' |
||
| 69 | 'associated_identifier' => $associated['identifier'], |
||
| 70 | 'connection_name' => $associated['connection_name'], |
||
| 71 | 'created_at' => now(), |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | DB::table('cache_invalidation_event_associations')->insert($associations); |
||
| 75 | } |
||
| 76 | */ |
||
| 77 | $insertOk = true; |
||
| 78 | //DB::commit(); // Completa la transazione |
||
| 79 | } catch (\Throwable $e) { |
||
| 80 | //DB::rollBack(); // Annulla la transazione in caso di errore |
||
| 81 | $attempts++; |
||
| 82 | Log::error("SuperCacheInvalidate: impossibile eseguire insert, tentativo $attempts di $maxAttempts: " . $e->getMessage()); |
||
| 83 | // Logica per gestire i tentativi falliti |
||
| 84 | if ($attempts >= $maxAttempts) { |
||
| 85 | // Salta il record dopo il numero massimo di tentativi |
||
| 86 | Log::error("SuperCacheInvalidate: impossibile eseguire insert dopo $maxAttempts tentativi: " . $e->getMessage()); |
||
| 87 | } |
||
| 203 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.