ldc-boutique-cnc-sable/app/Http/Controllers/Fo/Customers/CustomerController.php

74 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Customers;
use App\Actions\Crm\UpdateCustomer;
use App\Contracts\Executor;
2026-06-14 21:32:07 +02:00
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
use App\Models\Customer;
use App\Queries\Crm\GetCrmCustomerQuery;
use App\Queries\Crm\GetCrmCustomerQueryInput;
2026-06-14 21:32:07 +02:00
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Validation\ValidationException;
2026-06-14 21:32:07 +02:00
class CustomerController extends Controller
{
public function __construct(
private readonly Executor $executor,
2026-06-14 21:32:07 +02:00
) {
}
public function dashboard(): View
{
$this->authorize('manageOwnProfile', Customer::class);
return view('fo.customer.dashboard');
}
public function editInfos(GetCrmCustomerQuery $getCrmCustomerQuery): View
2026-06-14 21:32:07 +02:00
{
$this->authorize('manageOwnProfile', Customer::class);
$output = $this->executor->execute(
$getCrmCustomerQuery,
new GetCrmCustomerQueryInput()
);
2026-06-14 21:32:07 +02:00
abort_unless(
$output->customer instanceof CrmCustomer,
2026-06-14 21:32:07 +02:00
503,
trans('An error occurred, please try again later.')
);
return view('fo.customer.edit.infos', [
'crmCustomer' => $output->customer,
]);
2026-06-14 21:32:07 +02:00
}
public function editInfosSubmit(UpdateInfosRequest $request, UpdateCustomer $updateCustomer): RedirectResponse
2026-06-14 21:32:07 +02:00
{
$this->authorize('manageOwnProfile', Customer::class);
$output = $this->executor->execute(
$updateCustomer,
$request->toUpdateCustomerInput()
);
2026-06-14 21:32:07 +02:00
return match (true) {
$output->result instanceof CrmCustomer => back()
->with('success', trans('Your information has been saved.')),
$output->result instanceof ValidationException => back()
2026-06-14 21:32:07 +02:00
->withInput()
->withErrors($output->result->getMessage()),
default => back()
2026-06-14 21:32:07 +02:00
->withInput()
->withErrors(trans('An error occurred, please try again later.')),
};
2026-06-14 21:32:07 +02:00
}
}