Files
Atomcms-edit/app/Http/Controllers/User/GuestbookController.php
T
root 05fc7b04bc refactor: add return type hints to all controller methods
Added proper return types (View, RedirectResponse, JsonResponse, Collection)
to 40+ controller methods across 16 controllers. Also added missing
imports for Illuminate response types and tightened parameter types
(e.g. InstallationController::showStep now uses int instead of mixed).
2026-05-19 19:28:21 +02:00

58 lines
2.1 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\GuestbookFormRequest;
use App\Models\User;
use App\Models\User\WebsiteUserGuestbook;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
class GuestbookController extends Controller
{
public function store(User $user, GuestbookFormRequest $request): RedirectResponse
{
$this->validateGuestbookPost($user, $request);
$user->profileGuestbook()->create([
'user_id' => Auth::id(),
'message' => $request->input('message'),
]);
return redirect()->back()->with('success', __('Your message has been posted.'));
}
public function destroy(User $user, WebsiteUserGuestbook $guestbook): RedirectResponse
{
if ($guestbook->user_id !== Auth::id() && $guestbook->profile_id !== $user->id && Auth::user()->rank < (int) setting('min_staff_rank')) {
return redirect()->back()->withErrors([
'message' => __('Do do not have permission to delete this message'),
]);
}
$guestbook->delete();
return redirect()->back()->with('success', __('Your message has been deleted.'));
}
private function validateGuestbookPost(User $user, GuestbookFormRequest $request): ?RedirectResponse
{
if ($user->id === $request->user()->id) {
return $this->redirectWithError(__('You cannot post a message on your own profile.'));
}
$maxAllowedPostCount = empty(setting('max_guestbook_posts_per_profile')) ? 3 : (int) setting('max_guestbook_posts_per_profile');
if ($user->profileGuestbook()->where('user_id', $request->user()->id)->count() >= $maxAllowedPostCount) {
return $this->redirectWithError(__('You have already posted :count messages on this profile.', ['count' => $maxAllowedPostCount]));
}
}
private function redirectWithError(string $message): RedirectResponse
{
return redirect()->back()->withErrors(['message' => $message]);
}
}