Conditions | 9 |
Paths | 39 |
Total Lines | 77 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
24 | public function insertInvalidationEvent( |
||
25 | string $type, |
||
26 | string $identifier, |
||
27 | ?string $connection_name = null, |
||
28 | ?string $reason = null, |
||
29 | ?int $totalShards = 0, |
||
30 | ?int $priority = 0, |
||
31 | ?int $processed = 0, |
||
32 | ?Carbon $event_time = null, |
||
33 | /* ?array $associatedIdentifiers = [], */ |
||
34 | ?int $shard = -1, |
||
35 | ): void { |
||
36 | if ($shard < 0) { |
||
37 | $shard = crc32($identifier) % ($totalShards > 0 ? $totalShards : config('super_cache_invalidate.total_shards', 10)); |
||
38 | } |
||
39 | $redisConnectionName = $connection_name ?? config('super_cache_invalidate.default_connection_name'); |
||
40 | $data = [ |
||
41 | 'type' => $type, |
||
42 | 'identifier' => $identifier, |
||
43 | 'connection_name' => $redisConnectionName, |
||
44 | 'reason' => $reason, |
||
45 | 'priority' => $priority, |
||
46 | 'event_time' => $event_time ?? now(), |
||
47 | 'processed' => $processed, // ATTENZIONE, poichè abbiamo solo 2 priorità, nel caso di priorità 1 verrà passato 1 perchè l'invalidazione la fa il progetto |
||
48 | 'shard' => $shard, |
||
49 | ]; |
||
50 | |||
51 | $maxAttempts = 5; |
||
52 | $attempts = 0; |
||
53 | $insertOk = false; |
||
54 | |||
55 | while ($attempts < $maxAttempts && !$insertOk) { |
||
56 | // DB::beginTransaction(); |
||
57 | |||
58 | try { |
||
59 | // Cerca di bloccare il record per l'inserimento |
||
60 | switch ($processed) { |
||
61 | case 0: |
||
|
|||
62 | $partitionCache_invalidation_events = $this->getCacheInvalidationEventsUnprocessedPartitionName($shard, $priority); |
||
63 | break; |
||
64 | case 1: |
||
65 | $partitionCache_invalidation_events = $this->getCacheInvalidationEventsProcessedPartitionName($shard, $priority, $event_time ?? now()); |
||
66 | break; |
||
67 | default: |
||
68 | $attempts = 5; // Mi fermo |
||
69 | throw new \RuntimeException('Invalid value for processed'); |
||
70 | } |
||
71 | // $eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data); |
||
72 | DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insert($data); |
||
73 | // Insert associated identifiers |
||
74 | // TODO JB 31/12/2024: per adesso commentato, da riattivare quando tutto funziona alla perfezione usando la partizione, |
||
75 | // anche perchè la chiave primaria è data da id e partiton_key, per cui va capito cosa restituisce insertGetId e se è bloccante |
||
76 | /* |
||
77 | if (!empty($associatedIdentifiers)) { |
||
78 | $associations = []; |
||
79 | foreach ($associatedIdentifiers as $associated) { |
||
80 | $associations[] = [ |
||
81 | 'event_id' => $eventId, |
||
82 | 'associated_type' => $associated['type'], // 'key' or 'tag' |
||
83 | 'associated_identifier' => $associated['identifier'], |
||
84 | 'connection_name' => $associated['connection_name'], |
||
85 | 'created_at' => now(), |
||
86 | ]; |
||
87 | } |
||
88 | DB::table('cache_invalidation_event_associations')->insert($associations); |
||
89 | } |
||
90 | */ |
||
91 | $insertOk = true; |
||
92 | // DB::commit(); // Completa la transazione |
||
93 | } catch (\Throwable $e) { |
||
94 | // DB::rollBack(); // Annulla la transazione in caso di errore |
||
95 | $attempts++; |
||
96 | Log::error("SuperCacheInvalidate: impossibile eseguire insert, tentativo $attempts di $maxAttempts: " . $e->getMessage()); |
||
97 | // Logica per gestire i tentativi falliti |
||
98 | if ($attempts >= $maxAttempts) { |
||
99 | // Salta il record dopo il numero massimo di tentativi |
||
100 | Log::error("SuperCacheInvalidate: impossibile eseguire insert dopo $maxAttempts tentativi: " . $e->getMessage()); |
||
101 | } |
||
163 |