44
55final class IdempotencyConfig
66{
7- // Idempotency config keys
8- public const ENABLED_KEY = 'enabled ' ;
9- public const IDEMPOTENCY_HEADER_KEY = 'idempotency_header ' ;
10- public const RELAYED_HEADER_KEY = 'idempotency_relayed_header ' ;
11- public const ENFORCED_VERBS_KEY = 'enforced_verbs ' ;
12- public const DUPLICATE_HANDLING_KEY = 'duplicate_handling ' ;
13- public const MAX_LOCK_WAIT_TIME_KEY = 'max_lock_wait_time ' ;
14- public const USER_ID_RESOLVER_KEY = 'user_id_resolver ' ;
15- public const UNAUTHENTICATED_USER_ID_KEY = 'unauthenticated_user_id ' ;
16-
17- // Cache config keys
18- public const CACHE_TTL_KEY = 'cache.ttl ' ;
19- public const CACHE_STORE_KEY = 'cache.store ' ;
20-
217 // Default values
228 private const DEFAULT_MAX_LOCK_WAIT_TIME = 10 ; // 10 seconds
239 private const DEFAULT_CACHE_TTL = 86400 ; // 24 hours
2410 public const DEFAULT_CACHE_STORE = 'default ' ;
11+ public const DEFAULT_IDEMPOTENCY_HEADER = 'Idempotency-Key ' ;
12+ public const DEFAULT_RELAYED_HEADER = 'Idempotency-Relayed ' ;
13+ public const DEFAULT_ENFORCED_VERBS = ['POST ' , 'PUT ' , 'PATCH ' , 'DELETE ' ];
14+ public const DEFAULT_DUPLICATE_HANDLING = 'exception ' ;
15+ public const DEFAULT_UNAUTHENTICATED_USER_ID = 'guest ' ;
2516
2617 /**
2718 * @param array<string> $enforcedVerbs
@@ -40,22 +31,32 @@ private function __construct(
4031 private readonly string $ cacheStore
4132 ) {}
4233
43- // @phpstan-ignore-next-line
34+ /**
35+ * @param array{
36+ * enabled?: bool,
37+ * idempotency_header?: string,
38+ * idempotency_relayed_header?: string,
39+ * enforced_verbs?: string[],
40+ * duplicate_handling?: string,
41+ * max_lock_wait_time?: int,
42+ * user_id_resolver?: class-string|null,
43+ * unauthenticated_user_id?: string,
44+ * cache?: array{ttl?: int, store?: string}
45+ * } $attributes
46+ */
4447 public static function createFromArray (array $ attributes ): self
4548 {
46- $ get = static fn (string $ key , int |bool |string |array |null $ default = null ) => $ attributes [$ key ] ?? $ default ;
47-
4849 return new self (
49- $ get ( self :: ENABLED_KEY , false ) ,
50- $ get ( self ::IDEMPOTENCY_HEADER_KEY ) ,
51- $ get ( self ::RELAYED_HEADER_KEY ) ,
52- $ get ( self ::ENFORCED_VERBS_KEY ) ,
53- $ get ( self ::DUPLICATE_HANDLING_KEY ) ,
54- $ get ( self :: MAX_LOCK_WAIT_TIME_KEY , self ::DEFAULT_MAX_LOCK_WAIT_TIME ) ,
55- $ get ( self :: USER_ID_RESOLVER_KEY , null ) ,
56- $ get ( self ::UNAUTHENTICATED_USER_ID_KEY ) ,
57- $ get ( self :: CACHE_TTL_KEY , self ::DEFAULT_CACHE_TTL ) ,
58- $ get ( self :: CACHE_STORE_KEY , self ::DEFAULT_CACHE_STORE )
50+ enabled: $ attributes [ ' enabled ' ] ?? false ,
51+ idempotencyHeader: $ attributes [ ' idempotency_header ' ] ?? self ::DEFAULT_IDEMPOTENCY_HEADER ,
52+ relayedHeader: $ attributes [ ' idempotency_relayed_header ' ] ?? self ::DEFAULT_RELAYED_HEADER ,
53+ enforcedVerbs: $ attributes [ ' enforced_verbs ' ] ?? self ::DEFAULT_ENFORCED_VERBS ,
54+ duplicateHandling: $ attributes [ ' duplicate_handling ' ] ?? self ::DEFAULT_DUPLICATE_HANDLING ,
55+ maxLockWaitTime: $ attributes [ ' max_lock_wait_time ' ] ?? self ::DEFAULT_MAX_LOCK_WAIT_TIME ,
56+ userIdResolver: $ attributes [ ' user_id_resolver ' ] ?? null ,
57+ unauthenticatedUserId: $ attributes [ ' unauthenticated_user_id ' ] ?? self ::DEFAULT_UNAUTHENTICATED_USER_ID ,
58+ cacheTtl: $ attributes [ ' cache ' ][ ' ttl ' ] ?? self ::DEFAULT_CACHE_TTL ,
59+ cacheStore: $ attributes [ ' cache ' ][ ' store ' ] ?? self ::DEFAULT_CACHE_STORE ,
5960 );
6061 }
6162
@@ -117,7 +118,7 @@ public function getUnauthenticatedUserId(): string
117118
118119 public function getCacheTtl (int $ default = self ::DEFAULT_CACHE_TTL ): int
119120 {
120- return $ this ->cacheTtl ?? $ default ;
121+ return $ this ->cacheTtl > 0 ? $ this -> cacheTtl : $ default ;
121122 }
122123
123124 public function getCacheStore (): string
0 commit comments