Skip to content

Commit 976c307

Browse files
committed
Controlled api update
1 parent 19596ed commit 976c307

File tree

7 files changed

+70
-73
lines changed

7 files changed

+70
-73
lines changed

config/monitor.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,14 @@
311311
'duration_ms',
312312
'memory_mb',
313313

314-
// Controlled block keys (frequent in enterprise usage)
314+
// Controlled block keys
315315
'controlled_block',
316316
'controlled_block_id',
317317
'attempt',
318318
'status',
319319
'breaker_tripped',
320320
'escalated',
321321

322-
// Common business identifiers
323-
'name',
324322
'title',
325323
'type',
326324
'method',
@@ -361,6 +359,9 @@
361359
'session_id',
362360
'private_key',
363361
'client_secret',
362+
'full_name',
363+
'first_name',
364+
'last_name',
364365
'email',
365366
'ssn',
366367
'ein',
@@ -390,10 +391,6 @@
390391
'ssn' => '/\b\d{3}-?\d{2}-?\d{4}\b/',
391392
'credit_card' => '/\b(?:\d[ -]*?){13,16}\b/',
392393
'url_with_auth' => '/https?:\/\/[^:\/\s]+:[^@\/\s]+@[^\s]+/',
393-
394-
// Note: IPv4 removed (common pattern bypass handles this)
395-
// Note: Complex phone patterns removed (Shannon entropy will catch international)
396-
// Note: API key patterns removed (Shannon entropy is better for these)
397394
],
398395

399396
/*

src/Controlled.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function for(string $name): self
5959
*
6060
* @param array<string, mixed> $context
6161
*/
62-
public function context(array $context): self
62+
public function overrideContext(array $context): self
6363
{
6464
$this->context = $context;
6565

@@ -71,7 +71,7 @@ public function context(array $context): self
7171
*
7272
* @param array<string, mixed> $context
7373
*/
74-
public function with(array $context): self
74+
public function addContext(array $context): self
7575
{
7676
$this->context = array_merge($this->context, $context);
7777

@@ -92,7 +92,7 @@ public function escalated(Closure $callback): self
9292
return $this;
9393
}
9494

95-
public function breaker(string $name, int $threshold = 5, int $decaySeconds = 300): self
95+
public function withCircuitBreaker(string $name, int $threshold = 5, int $decaySeconds = 300): self
9696
{
9797
$this->breakerName = $name;
9898
$this->breakerThreshold = $threshold;
@@ -105,7 +105,7 @@ public function breaker(string $name, int $threshold = 5, int $decaySeconds = 30
105105
* @param array<class-string<\Throwable>> $only
106106
* @param array<class-string<\Throwable>> $exclude
107107
*/
108-
public function transactioned(int $retries = 0, array $only = [], array $exclude = []): self
108+
public function withDatabaseTransaction(int $retries = 0, array $only = [], array $exclude = []): self
109109
{
110110
$this->withTransaction = true;
111111
$this->transactionRetries = $retries;
@@ -115,7 +115,7 @@ public function transactioned(int $retries = 0, array $only = [], array $exclude
115115
return $this;
116116
}
117117

118-
public function traceId(string $traceId): self
118+
public function overrideTraceId(string $traceId): self
119119
{
120120
$this->traceIdOverride = $traceId;
121121

tests/Feature/ControlledFeatureTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@
5656
$context = ['user_id' => 123, 'action' => 'test'];
5757

5858
$result = Controlled::for('context-test')
59-
->context($context)
59+
->overrideContext($context)
6060
->run(fn () => 'context-success');
6161

6262
expect($result)->toBe('context-success');
6363
});
6464

6565
it('merges context and executes', function () {
6666
$result = Controlled::for('merge-context-test')
67-
->context(['initial' => 'value'])
68-
->with(['additional' => 'data'])
67+
->overrideContext(['initial' => 'value'])
68+
->addContext(['additional' => 'data'])
6969
->run(fn () => 'merge-success');
7070

7171
expect($result)->toBe('merge-success');
@@ -134,15 +134,15 @@
134134

135135
it('configures circuit breaker with defaults', function () {
136136
$result = Controlled::for('breaker-test')
137-
->breaker('test-breaker')
137+
->withCircuitBreaker('test-breaker')
138138
->run(fn () => 'success');
139139

140140
expect($result)->toBe('success');
141141
});
142142

143143
it('configures circuit breaker with custom threshold and decay', function () {
144144
$result = Controlled::for('custom-breaker-test')
145-
->breaker('custom-breaker', 10, 600)
145+
->withCircuitBreaker('custom-breaker', 10, 600)
146146
->run(fn () => 'success');
147147

148148
expect($result)->toBe('success');
@@ -155,7 +155,7 @@
155155
for ($i = 0; $i < 5; $i++) {
156156
try {
157157
Controlled::for("failure-{$i}")
158-
->breaker($breakerName)
158+
->withCircuitBreaker($breakerName)
159159
->run(function () {
160160
throw new RuntimeException('Simulated failure');
161161
});
@@ -167,7 +167,7 @@
167167
// Next call should be blocked by circuit breaker
168168
expect(function () use ($breakerName) {
169169
Controlled::for('blocked-test')
170-
->breaker($breakerName)
170+
->withCircuitBreaker($breakerName)
171171
->run(fn () => 'should-not-execute');
172172
})->toThrow(RuntimeException::class, "Circuit breaker '{$breakerName}' is open");
173173
});
@@ -186,7 +186,7 @@
186186

187187
// Now execution should succeed and keep breaker closed
188188
$result = Controlled::for('reset-success-test')
189-
->breaker($breakerName)
189+
->withCircuitBreaker($breakerName)
190190
->run(fn () => 'success');
191191

192192
expect($result)->toBe('success')
@@ -204,7 +204,7 @@
204204
});
205205

206206
$result = Controlled::for('transaction-test')
207-
->transactioned()
207+
->withDatabaseTransaction()
208208
->run(fn () => 'transaction-success');
209209

210210
expect($result)->toBe('transaction-success');
@@ -226,7 +226,7 @@
226226
});
227227

228228
$result = Controlled::for('retry-test')
229-
->transactioned(2) // 2 retries
229+
->withDatabaseTransaction(2) // 2 retries
230230
->run(fn () => 'retry-success');
231231

232232
expect($result)->toBe('retry-success')
@@ -239,7 +239,7 @@
239239
$customTraceId = 'custom-trace-123';
240240

241241
$result = Controlled::for('trace-override-test')
242-
->traceId($customTraceId)
242+
->overrideTraceId($customTraceId)
243243
->run(fn () => 'trace-success');
244244

245245
expect($result)->toBe('trace-success');
@@ -289,9 +289,9 @@
289289
describe('Full Integration Chaining', function () {
290290
it('supports fluent interface chaining with execution', function () {
291291
$result = Controlled::for('chain-test')
292-
->context(['initial' => 'data'])
293-
->with(['additional' => 'context'])
294-
->traceId('custom-trace')
292+
->overrideContext(['initial' => 'data'])
293+
->addContext(['additional' => 'context'])
294+
->overrideTraceId('custom-trace')
295295
->from('ChainTest')
296296
->run(fn () => 'chain-success');
297297

tests/Feature/ControlledIntegrationTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
app()->instance(CircuitBreaker::class, new CircuitBreaker);
4545

4646
$result = Monitor::controlled('breaker-integration')
47-
->breaker('test-integration-breaker')
47+
->withCircuitBreaker('test-integration-breaker')
4848
->run(fn () => 'breaker-success');
4949

5050
expect($result)->toBe('breaker-success');
@@ -59,16 +59,16 @@
5959
});
6060

6161
$result = Monitor::controlled('transaction-integration')
62-
->transactioned()
62+
->withDatabaseTransaction()
6363
->run(fn () => 'transaction-success');
6464

6565
expect($result)->toBe('transaction-success');
6666
});
6767

6868
it('chains multiple configuration methods through facade', function () {
6969
$result = Monitor::controlled('full-chain-test')
70-
->context(['feature' => 'integration'])
71-
->with(['test' => 'comprehensive'])
70+
->overrideContext(['feature' => 'integration'])
71+
->addContext(['test' => 'comprehensive'])
7272

7373
->from('IntegrationTest')
7474
->run(fn () => 'full-chain-success');
@@ -114,7 +114,7 @@
114114
$customTraceId = 'custom-integration-trace-123';
115115

116116
$result = Monitor::controlled('trace-override-integration')
117-
->traceId($customTraceId)
117+
->overrideTraceId($customTraceId)
118118
->run(fn () => 'trace-override-success');
119119

120120
expect($result)->toBe('trace-override-success');
@@ -156,7 +156,7 @@ public function __toString(): string
156156
$result = Monitor::from('StringOrigin')
157157
->controlled()
158158
->for('string-origin-test')
159-
->context(['test' => 'origin-preservation'])
159+
->overrideContext(['test' => 'origin-preservation'])
160160
->run(fn () => 'origin-chain-success');
161161

162162
expect($result)->toBe('origin-chain-success');
@@ -168,7 +168,7 @@ public function __toString(): string
168168
$paymentSuccessful = false;
169169

170170
$result = Monitor::controlled('payment_processing')
171-
->context([
171+
->overrideContext([
172172
'user_id' => 123,
173173
'amount' => 99.99,
174174
'currency' => 'USD',
@@ -211,10 +211,10 @@ public function __toString(): string
211211
});
212212

213213
$result = Monitor::controlled('database_critical_write')
214-
->context(['operation' => 'user_signup', 'table' => 'users'])
214+
->overrideContext(['operation' => 'user_signup', 'table' => 'users'])
215215

216-
->breaker('database_write', 3, 60)
217-
->transactioned(1) // 1 retry
216+
->withCircuitBreaker('database_write', 3, 60)
217+
->withDatabaseTransaction(1) // 1 retry
218218
->failing(function ($exception, $meta) {
219219
// Log database issues for monitoring
220220
})
@@ -229,13 +229,13 @@ public function __toString(): string
229229
$apiCallCount = 0;
230230

231231
$result = Monitor::controlled('external_api_integration')
232-
->context([
232+
->overrideContext([
233233
'service' => 'stripe',
234234
'endpoint' => '/charges',
235235
'method' => 'POST',
236236
])
237237

238-
->traceId('api-call-trace-789')
238+
->overrideTraceId('api-call-trace-789')
239239
->failing(function ($exception, $meta) use (&$apiCallCount) {
240240
// Increment failure metrics
241241
$apiCallCount++;

0 commit comments

Comments
 (0)