refactor: extract workspace validation to middleware #13

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

Code Quality Issue

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

Problem

The same workspace validation pattern is duplicated across 8 methods in WebhookSecretController:

$workspace = $request->user()?->defaultHostWorkspace();
if (! $workspace) {
    return response()->json(['error' => 'Workspace not found'], 404);
}

Occurrences: Lines 28-32, 66-70, 104-108, 128-132, 152-156, 178-182, 206-210, 240-244

Proposed Solution

Create a new middleware RequireWorkspace that:

  1. Validates workspace exists for authenticated user
  2. Attaches workspace to request for controller use
  3. Returns 404 if workspace not found

Benefits:

  • DRY principle (Don't Repeat Yourself)
  • Centralized error handling
  • Easier to maintain and modify
  • Reusable across other controllers

Implementation

// src/Api/Middleware/RequireWorkspace.php
class RequireWorkspace
{
    public function handle(Request $request, Closure $next)
    {
        $workspace = $request->user()?->defaultHostWorkspace();
        if (! $workspace) {
            return response()->json(['error' => 'Workspace not found'], 404);
        }
        $request->merge(['workspace' => $workspace]);
        return $next($request);
    }
}

Then apply to route group or individual controller methods.

Priority

Medium - Code quality improvement, not a bug

## Code Quality Issue **File**: `src/Api/Controllers/Api/WebhookSecretController.php` ### Problem The same workspace validation pattern is duplicated across 8 methods in WebhookSecretController: ```php $workspace = $request->user()?->defaultHostWorkspace(); if (! $workspace) { return response()->json(['error' => 'Workspace not found'], 404); } ``` **Occurrences**: Lines 28-32, 66-70, 104-108, 128-132, 152-156, 178-182, 206-210, 240-244 ### Proposed Solution Create a new middleware `RequireWorkspace` that: 1. Validates workspace exists for authenticated user 2. Attaches workspace to request for controller use 3. Returns 404 if workspace not found **Benefits**: - DRY principle (Don't Repeat Yourself) - Centralized error handling - Easier to maintain and modify - Reusable across other controllers ### Implementation ```php // src/Api/Middleware/RequireWorkspace.php class RequireWorkspace { public function handle(Request $request, Closure $next) { $workspace = $request->user()?->defaultHostWorkspace(); if (! $workspace) { return response()->json(['error' => 'Workspace not found'], 404); } $request->merge(['workspace' => $workspace]); return $next($request); } } ``` Then apply to route group or individual controller methods. ### Priority Medium - Code quality improvement, not a bug
Clotho added the
discovery
label 2026-02-20 03:16:14 +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.