Conditions | 8 |
Paths | 36 |
Total Lines | 57 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 insertInvalidationEvent( |
||
23 | string $type, |
||
24 | string $identifier, |
||
25 | ?string $connection_name = null, |
||
26 | ?string $reason = null, |
||
27 | ?int $totalShards = 0, |
||
28 | ?int $priority = 0, |
||
29 | ?array $associatedIdentifiers = [], |
||
30 | ): void { |
||
31 | $shard = crc32($identifier) % ($totalShards > 0 ? $totalShards : config('super_cache_invalidate.total_shards', 10)); |
||
32 | |||
33 | $redisConnectionName = $connection_name ?? config('super_cache_invalidate.default_connection_name'); |
||
34 | $data = [ |
||
35 | 'type' => $type, |
||
36 | 'identifier' => $identifier, |
||
37 | 'connection_name' => $redisConnectionName, |
||
38 | 'reason' => $reason, |
||
39 | 'priority' => $priority, |
||
40 | 'event_time' => now(), |
||
41 | 'processed' => 0, |
||
42 | 'shard' => $shard, |
||
43 | ]; |
||
44 | |||
45 | $maxAttempts = 5; |
||
46 | $attempts = 0; |
||
47 | $insertOk = false; |
||
48 | |||
49 | while ($attempts < $maxAttempts && !$insertOk) { |
||
50 | DB::beginTransaction(); |
||
51 | |||
52 | try { |
||
53 | // Cerca di bloccare il record per l'inserimento |
||
54 | $eventId = DB::table('cache_invalidation_events')->insertGetId($data); |
||
55 | |||
56 | // Insert associated identifiers |
||
57 | if (!empty($associatedIdentifiers)) { |
||
58 | $associations = []; |
||
59 | foreach ($associatedIdentifiers as $associated) { |
||
60 | $associations[] = [ |
||
61 | 'event_id' => $eventId, |
||
62 | 'associated_type' => $associated['type'], // 'key' or 'tag' |
||
63 | 'associated_identifier' => $associated['identifier'], |
||
64 | 'connection_name' => $associated['connection_name'], |
||
65 | 'created_at' => now(), |
||
66 | ]; |
||
67 | } |
||
68 | DB::table('cache_invalidation_event_associations')->insert($associations); |
||
69 | } |
||
70 | $insertOk = true; |
||
71 | DB::commit(); // Completa la transazione |
||
72 | } catch (\Throwable $e) { |
||
73 | DB::rollBack(); // Annulla la transazione in caso di errore |
||
74 | $attempts++; |
||
75 | // Logica per gestire i tentativi falliti |
||
76 | if ($attempts >= $maxAttempts) { |
||
77 | // Salta il record dopo il numero massimo di tentativi |
||
78 | Log::error("SuperCacheInvalidate: impossibile eseguire insert dopo $maxAttempts tentativi: " . $e->getMessage()); |
||
79 | } |
||
127 |