You've already forked Atomcms-edit
refactor: improve code quality across controllers and services
- DRY FurniEditorController: extract duplicate try/catch blocks into handleApiError(), formatItemData(), buildUpdateData(), buildInsertData(), castValue() methods - ProfileController: replace 45 lines of manual date formatting with Carbon's diffForHumans() - Replace custom Password rule (180 lines) with Laravel's built-in Password::min() rule - RadioController: extract RadioStreamService and RadioScheduleService, reducing from 608 to 323 lines - Add RadioSettings enum to replace magic strings throughout radio feature - Add CurrencyTypes::columnName() helper method - Add consistent return types (JsonResponse, View, RedirectResponse) to all controller methods
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -17,7 +18,63 @@ class FurniEditorController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
public function search(Request $request)
|
||||
private function handleApiError(string $action, \Exception $e): JsonResponse
|
||||
{
|
||||
Log::error("[FurniEditor] {$action} failed", [
|
||||
'error' => $e->getMessage(),
|
||||
'user_id' => Auth::id(),
|
||||
]);
|
||||
|
||||
return response()->json(['error' => "{$action} mislukt"], 500);
|
||||
}
|
||||
|
||||
private function formatItemData(\stdClass $item): array
|
||||
{
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'spriteId' => $item->sprite_id,
|
||||
'itemName' => $item->item_name,
|
||||
'publicName' => $item->public_name,
|
||||
'type' => $item->type,
|
||||
'width' => $item->width,
|
||||
'length' => $item->length,
|
||||
'stackHeight' => $item->stack_height,
|
||||
'allowStack' => (bool) $item->allow_stack,
|
||||
'allowWalk' => (bool) $item->allow_walk,
|
||||
'allowSit' => (bool) $item->allow_sit,
|
||||
'allowLay' => (bool) $item->allow_lay,
|
||||
'allowGift' => (bool) ($item->allow_gift ?? false),
|
||||
'allowTrade' => (bool) ($item->allow_trade ?? false),
|
||||
'allowRecycle' => (bool) ($item->allow_recycle ?? false),
|
||||
'allowMarketplaceSell' => (bool) ($item->allow_marketplace_sell ?? false),
|
||||
'allowInventoryStack' => (bool) ($item->allow_inventory_stack ?? true),
|
||||
'interactionType' => $item->interaction_type,
|
||||
'interactionModesCount' => $item->interaction_modes_count,
|
||||
'vendingIds' => $item->vending_ids ?? '',
|
||||
'multiheight' => $item->multiheight ?? '',
|
||||
'customparams' => $item->customparams ?? '',
|
||||
'effectIdMale' => $item->effect_id_male ?? 0,
|
||||
'effectIdFemale' => $item->effect_id_female ?? 0,
|
||||
'clothingOnWalk' => $item->clothing_on_walk ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
private function formatCatalogItemData(\stdClass $r): array
|
||||
{
|
||||
return [
|
||||
'id' => $r->id,
|
||||
'catalogName' => $r->catalog_name,
|
||||
'costCredits' => $r->cost_credits,
|
||||
'costPoints' => $r->cost_points,
|
||||
'pointsType' => $r->points_type,
|
||||
'pageId' => $r->page_id,
|
||||
'pageName' => $r->page_id
|
||||
? DB::table('catalog_pages')->where('id', $r->page_id)->value('caption') ?? ''
|
||||
: '',
|
||||
];
|
||||
}
|
||||
|
||||
public function search(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -70,14 +127,12 @@ class FurniEditorController extends Controller
|
||||
'total' => $total,
|
||||
'page' => $page,
|
||||
]);
|
||||
} catch (\Exception) {
|
||||
Log::error('[FurniEditor] Search failed', ['error' => 'Actie mislukt']);
|
||||
|
||||
return response()->json(['error' => 'Zoeken mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Zoeken', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function detail(Request $request)
|
||||
public function detail(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -95,55 +150,22 @@ class FurniEditorController extends Controller
|
||||
$catalog = DB::table('catalog_items')
|
||||
->whereRaw('FIND_IN_SET(?, item_ids)', [$id])
|
||||
->get()
|
||||
->map(fn ($r) => [
|
||||
'id' => $r->id,
|
||||
'catalogName' => $r->catalog_name,
|
||||
'costCredits' => $r->cost_credits,
|
||||
'costPoints' => $r->cost_points,
|
||||
'pointsType' => $r->points_type,
|
||||
'pageId' => $r->page_id,
|
||||
'pageName' => $r->page_id ? DB::table('catalog_pages')->where('id', $r->page_id)->value('caption') ?? '' : '',
|
||||
]);
|
||||
->map(fn ($r) => $this->formatCatalogItemData($r));
|
||||
|
||||
return response()->json([
|
||||
'item' => [
|
||||
'id' => $item->id,
|
||||
'spriteId' => $item->sprite_id,
|
||||
'itemName' => $item->item_name,
|
||||
'publicName' => $item->public_name,
|
||||
'type' => $item->type,
|
||||
'width' => $item->width,
|
||||
'length' => $item->length,
|
||||
'stackHeight' => $item->stack_height,
|
||||
'allowStack' => (bool) $item->allow_stack,
|
||||
'allowWalk' => (bool) $item->allow_walk,
|
||||
'allowSit' => (bool) $item->allow_sit,
|
||||
'allowLay' => (bool) $item->allow_lay,
|
||||
'allowGift' => (bool) $item->allow_gift,
|
||||
'allowTrade' => (bool) $item->allow_trade,
|
||||
'allowRecycle' => (bool) $item->allow_recycle,
|
||||
'allowMarketplaceSell' => (bool) $item->allow_marketplace_sell,
|
||||
'allowInventoryStack' => (bool) $item->allow_inventory_stack,
|
||||
'interactionType' => $item->interaction_type,
|
||||
'interactionModesCount' => $item->interaction_modes_count,
|
||||
'vendingIds' => $item->vending_ids,
|
||||
'multiheight' => $item->multiheight,
|
||||
'customparams' => $item->customparams,
|
||||
'effectIdMale' => $item->effect_id_male,
|
||||
'effectIdFemale' => $item->effect_id_female,
|
||||
'clothingOnWalk' => $item->clothing_on_walk,
|
||||
'item' => array_merge($this->formatItemData($item), [
|
||||
'description' => '',
|
||||
'usageCount' => 0,
|
||||
],
|
||||
]),
|
||||
'catalogItems' => $catalog,
|
||||
'furniDataEntry' => null,
|
||||
]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Kan item niet laden'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Item laden', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function update(Request $request)
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -154,48 +176,7 @@ class FurniEditorController extends Controller
|
||||
}
|
||||
|
||||
$data = $request->json()->all();
|
||||
$update = [];
|
||||
|
||||
$map = [
|
||||
'itemName' => ['item_name', 'string', 100],
|
||||
'publicName' => ['public_name', 'string', 100],
|
||||
'spriteId' => ['sprite_id', 'integer', 0],
|
||||
'width' => ['width', 'integer', 0],
|
||||
'length' => ['length', 'integer', 0],
|
||||
'stackHeight' => ['stack_height', 'integer', 0],
|
||||
'allowStack' => ['allow_stack', 'boolean', 0],
|
||||
'allowWalk' => ['allow_walk', 'boolean', 0],
|
||||
'allowSit' => ['allow_sit', 'boolean', 0],
|
||||
'allowLay' => ['allow_lay', 'boolean', 0],
|
||||
'allowGift' => ['allow_gift', 'boolean', 0],
|
||||
'allowTrade' => ['allow_trade', 'boolean', 0],
|
||||
'allowRecycle' => ['allow_recycle', 'boolean', 0],
|
||||
'allowMarketplaceSell' => ['allow_marketplace_sell', 'boolean', 0],
|
||||
'allowInventoryStack' => ['allow_inventory_stack', 'boolean', 0],
|
||||
'interactionType' => ['interaction_type', 'string', 50],
|
||||
'interactionModesCount' => ['interaction_modes_count', 'integer', 0],
|
||||
'vendingIds' => ['vending_ids', 'string', 255],
|
||||
'multiheight' => ['multiheight', 'string', 255],
|
||||
'customparams' => ['customparams', 'string', 255],
|
||||
'effectIdMale' => ['effect_id_male', 'integer', 0],
|
||||
'effectIdFemale' => ['effect_id_female', 'integer', 0],
|
||||
'clothingOnWalk' => ['clothing_on_walk', 'string', 255],
|
||||
];
|
||||
|
||||
foreach ($map as $key => [$col, $type, $maxLen]) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
$val = $data[$key];
|
||||
if ($type === 'string') {
|
||||
$val = mb_substr(strip_tags((string) $val), 0, $maxLen);
|
||||
} elseif ($type === 'integer') {
|
||||
$val = (int) $val;
|
||||
} elseif ($type === 'boolean') {
|
||||
$val = (bool) $val;
|
||||
}
|
||||
$update[$col] = $val;
|
||||
}
|
||||
$update = $this->buildUpdateData($data);
|
||||
|
||||
if ($update === []) {
|
||||
return response()->json(['error' => 'No fields to update'], 400);
|
||||
@@ -210,38 +191,19 @@ class FurniEditorController extends Controller
|
||||
]);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Actie mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Actie', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(Request $request)
|
||||
public function create(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
try {
|
||||
$data = $request->json()->all();
|
||||
|
||||
$id = DB::table('items_base')->insertGetId([
|
||||
'sprite_id' => $data['spriteId'] ?? 0,
|
||||
'item_name' => $data['itemName'] ?? '',
|
||||
'public_name' => $data['publicName'] ?? '',
|
||||
'type' => $data['type'] ?? 's',
|
||||
'width' => $data['width'] ?? 1,
|
||||
'length' => $data['length'] ?? 1,
|
||||
'stack_height' => $data['stackHeight'] ?? 0,
|
||||
'allow_stack' => $data['allowStack'] ?? false,
|
||||
'allow_walk' => $data['allowWalk'] ?? false,
|
||||
'allow_sit' => $data['allowSit'] ?? false,
|
||||
'allow_lay' => $data['allowLay'] ?? false,
|
||||
'allow_gift' => $data['allowGift'] ?? true,
|
||||
'allow_trade' => $data['allowTrade'] ?? true,
|
||||
'allow_recycle' => $data['allowRecycle'] ?? false,
|
||||
'allow_marketplace_sell' => $data['allowMarketplaceSell'] ?? false,
|
||||
'allow_inventory_stack' => $data['allowInventoryStack'] ?? true,
|
||||
'interaction_type' => $data['interactionType'] ?? 'default',
|
||||
'interaction_modes_count' => $data['interactionModesCount'] ?? 1,
|
||||
]);
|
||||
$id = DB::table('items_base')->insertGetId($this->buildInsertData($data));
|
||||
|
||||
Log::info('[Audit] FurniEditor create', [
|
||||
'user_id' => Auth::id(),
|
||||
@@ -249,12 +211,12 @@ class FurniEditorController extends Controller
|
||||
]);
|
||||
|
||||
return response()->json(['id' => $id]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Actie mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Actie', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete(Request $request)
|
||||
public function delete(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -272,12 +234,12 @@ class FurniEditorController extends Controller
|
||||
]);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Actie mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Actie', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function interactions(Request $request)
|
||||
public function interactions(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -289,12 +251,12 @@ class FurniEditorController extends Controller
|
||||
->pluck('interaction_type');
|
||||
|
||||
return response()->json(['interactions' => $rows]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Actie mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Actie', $e);
|
||||
}
|
||||
}
|
||||
|
||||
public function bySprite(Request $request)
|
||||
public function bySprite(Request $request): JsonResponse
|
||||
{
|
||||
$this->checkAdmin();
|
||||
|
||||
@@ -310,8 +272,90 @@ class FurniEditorController extends Controller
|
||||
}
|
||||
|
||||
return response()->json(['id' => $item->id]);
|
||||
} catch (\Exception) {
|
||||
return response()->json(['error' => 'Actie mislukt'], 500);
|
||||
} catch (\Exception $e) {
|
||||
return $this->handleApiError('Actie', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function buildUpdateData(array $data): array
|
||||
{
|
||||
$map = [
|
||||
'itemName' => ['item_name', 'string', 100],
|
||||
'publicName' => ['public_name', 'string', 100],
|
||||
'spriteId' => ['sprite_id', 'integer', 0],
|
||||
'width' => ['width', 'integer', 0],
|
||||
'length' => ['length', 'integer', 0],
|
||||
'stackHeight' => ['stack_height', 'integer', 0],
|
||||
'allowStack' => ['allow_stack', 'boolean', 0],
|
||||
'allowWalk' => ['allow_walk', 'boolean', 0],
|
||||
'allowSit' => ['allow_sit', 'boolean', 0],
|
||||
'allowLay' => ['allow_lay', 'boolean', 0],
|
||||
'allowGift' => ['allow_gift', 'boolean', 0],
|
||||
'allowTrade' => ['allow_trade', 'boolean', 0],
|
||||
'allowRecycle' => ['allow_recycle', 'boolean', 0],
|
||||
'allowMarketplaceSell' => ['allow_marketplace_sell', 'boolean', 0],
|
||||
'allowInventoryStack' => ['allow_inventory_stack', 'boolean', 0],
|
||||
'interactionType' => ['interaction_type', 'string', 50],
|
||||
'interactionModesCount' => ['interaction_modes_count', 'integer', 0],
|
||||
'vendingIds' => ['vending_ids', 'string', 255],
|
||||
'multiheight' => ['multiheight', 'string', 255],
|
||||
'customparams' => ['customparams', 'string', 255],
|
||||
'effectIdMale' => ['effect_id_male', 'integer', 0],
|
||||
'effectIdFemale' => ['effect_id_female', 'integer', 0],
|
||||
'clothingOnWalk' => ['clothing_on_walk', 'string', 255],
|
||||
];
|
||||
|
||||
$update = [];
|
||||
|
||||
foreach ($map as $key => [$col, $type, $maxLen]) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
$update[$col] = $this->castValue($data[$key], $type, $maxLen);
|
||||
}
|
||||
|
||||
return $update;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function buildInsertData(array $data): array
|
||||
{
|
||||
return [
|
||||
'sprite_id' => $data['spriteId'] ?? 0,
|
||||
'item_name' => $data['itemName'] ?? '',
|
||||
'public_name' => $data['publicName'] ?? '',
|
||||
'type' => $data['type'] ?? 's',
|
||||
'width' => $data['width'] ?? 1,
|
||||
'length' => $data['length'] ?? 1,
|
||||
'stack_height' => $data['stackHeight'] ?? 0,
|
||||
'allow_stack' => $data['allowStack'] ?? false,
|
||||
'allow_walk' => $data['allowWalk'] ?? false,
|
||||
'allow_sit' => $data['allowSit'] ?? false,
|
||||
'allow_lay' => $data['allowLay'] ?? false,
|
||||
'allow_gift' => $data['allowGift'] ?? true,
|
||||
'allow_trade' => $data['allowTrade'] ?? true,
|
||||
'allow_recycle' => $data['allowRecycle'] ?? false,
|
||||
'allow_marketplace_sell' => $data['allowMarketplaceSell'] ?? false,
|
||||
'allow_inventory_stack' => $data['allowInventoryStack'] ?? true,
|
||||
'interaction_type' => $data['interactionType'] ?? 'default',
|
||||
'interaction_modes_count' => $data['interactionModesCount'] ?? 1,
|
||||
];
|
||||
}
|
||||
|
||||
private function castValue(mixed $value, string $type, int $maxLen = 0): mixed
|
||||
{
|
||||
return match ($type) {
|
||||
'string' => mb_substr(strip_tags((string) $value), 0, $maxLen),
|
||||
'integer' => (int) $value,
|
||||
'boolean' => (bool) $value,
|
||||
default => $value,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user