Files
Atomcms-edit/app/Http/Controllers/Shop/ShopVoucherController.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

46 lines
1.5 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Shop;
use App\Http\Controllers\Controller;
use App\Http\Requests\ShopVoucherFormRequest;
use App\Models\Shop\WebsiteShopVoucher;
use Illuminate\Http\RedirectResponse;
class ShopVoucherController extends Controller
{
public function __invoke(ShopVoucherFormRequest $request): RedirectResponse
{
$user = $request->user();
$voucher = WebsiteShopVoucher::where('code', $request->string('code'))->first();
if (is_null($voucher) || ($voucher->expires_at && $voucher->expires_at->lte(now()))) {
return redirect()->back()->withErrors([
'message' => __('No active voucher with the given code was found'),
]);
}
if ($user->usedShopVouchers()->where('voucher_id', $voucher->id)->exists()) {
return redirect()->back()->withErrors([
'message' => __('You can only use each shop voucher once'),
]);
}
$user->usedShopVouchers()->create([
'voucher_id' => $voucher->id,
]);
$user->increment('website_balance', $voucher->amount);
$voucher->increment('use_count');
if ($voucher->max_uses && $voucher->use_count >= $voucher->max_uses) {
$voucher->update([
'expires_at' => now(),
]);
}
return redirect()->back()->with('success', __('Your balance has been increased by $:amount', ['amount' => $voucher->amount]));
}
}