refactor: deduplicate webhook lookup pattern #14

Open
opened 2026-02-20 03:16:37 +00:00 by Clotho · 0 comments
Member

Code Quality Issue

File: src/Api/Controllers/Api/WebhookSecretController.php

Problem

The same webhook lookup pattern is repeated 7 times across different methods:

$webhook = Model::where('workspace_id', $workspace->id)
    ->where('uuid', $uuid)
    ->first();

if (! $webhook) {
    return response()->json(['error' => 'Webhook not found'], 404);
}

Occurrences: Lines 34-40, 72-78, 110-116, 158-164, 185-191, 212-218, 246-252

Issue: Different models used (Webhook, ContentWebhookEndpoint) in different methods.

Proposed Solution

Option 1: Route Model Binding with custom resolver

// In route definition
Route::post('/{webhook:uuid}/rotate', [WebhookSecretController::class, 'rotate']);

// In RouteServiceProvider or model
public function resolveRouteBinding($value, $field = null)
{
    return $this->where($field ?? 'uuid', $value)
        ->where('workspace_id', auth()->user()->defaultHostWorkspace()->id)
        ->firstOrFail();
}

Option 2: Helper method in controller

protected function findWebhookOrFail(Request $request, string $uuid, string $modelClass)
{
    $workspace = $request->user()->defaultHostWorkspace();
    return $modelClass::where('workspace_id', $workspace->id)
        ->where('uuid', $uuid)
        ->firstOrFail();
}

Priority

Medium - Code quality improvement

## Code Quality Issue **File**: `src/Api/Controllers/Api/WebhookSecretController.php` ### Problem The same webhook lookup pattern is repeated 7 times across different methods: ```php $webhook = Model::where('workspace_id', $workspace->id) ->where('uuid', $uuid) ->first(); if (! $webhook) { return response()->json(['error' => 'Webhook not found'], 404); } ``` **Occurrences**: Lines 34-40, 72-78, 110-116, 158-164, 185-191, 212-218, 246-252 **Issue**: Different models used (Webhook, ContentWebhookEndpoint) in different methods. ### Proposed Solution Option 1: **Route Model Binding** with custom resolver ```php // In route definition Route::post('/{webhook:uuid}/rotate', [WebhookSecretController::class, 'rotate']); // In RouteServiceProvider or model public function resolveRouteBinding($value, $field = null) { return $this->where($field ?? 'uuid', $value) ->where('workspace_id', auth()->user()->defaultHostWorkspace()->id) ->firstOrFail(); } ``` Option 2: **Helper method** in controller ```php protected function findWebhookOrFail(Request $request, string $uuid, string $modelClass) { $workspace = $request->user()->defaultHostWorkspace(); return $modelClass::where('workspace_id', $workspace->id) ->where('uuid', $uuid) ->firstOrFail(); } ``` ### Priority Medium - Code quality improvement
Clotho added the
discovery
label 2026-02-20 03:16:37 +00:00
Charon added
PHP
refactor
P2
and removed
discovery
labels 2026-02-20 12:17:07 +00:00
Clotho was assigned by Charon 2026-02-20 12:21:03 +00:00
Charon added the
agent-ready
label 2026-02-21 01:30:29 +00:00
Sign in to join this conversation.
No description provided.