ldc-gen-game-web/app/Http/Requests/Fo/Customers/CreateAccountRequest.php
2026-10-20 12:27:49 +02:00

216 lines
8.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Fo\Customers;
use App\Models\Customer;
use App\Models\Settings;
use App\Repositories\Fo\CrmRepository;
use App\Rules\IsAdult;
use Closure;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\Rules\Password;
use LDC\LDCCRM\Enums\Country;
use LDC\LDCCRM\Models\Customer as CrmCustomer;
class CreateAccountRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return Auth::guard('frontend')->check();
}
/**
* Prepare the data for validation.
*/
public function prepareForValidation(): void
{
$this->merge([
'phone' => $this->input('phone'),
'opt_in' => $this->boolean('opt_in'),
'ok_cgv' => $this->boolean('ok_cgv'),
]);
}
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
/** @var Settings $settings */
$settings = resolve(Settings::class);
/** @var \App\Models\Customer|null */
$authUser = auth('frontend')->user();
$countryCode = $this->integer('country') != 0 ?
$this->integer('country') : ($authUser ?
new CrmRepository()->fetchCrmCustomer($authUser)->country->value : 0);
$countryRules = match ($countryCode) {
Country::belgium->value => [
'phone' => 'phone:BE',
'postal_code' => 'postal_code:BE',
],
Country::luxembourg->value => [
'phone' => 'phone:LU',
'postal_code' => 'postal_code:LU',
],
Country::germany->value => [
'phone' => 'phone:DE',
'postal_code' => 'postal_code:DE',
],
default => [
'phone' => 'phone:FR',
'postal_code' => 'postal_code:FR',
]
};
return [
'lastname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'phone' => [
'nullable',
'max:255',
$countryRules['phone'],
],
'address' => 'required|string|min:1|max:255',
'address2' => 'nullable|string|min:1|max:255',
'postal_code' => [
'required',
'string',
'min:1',
$countryRules['postal_code'],
],
'city' => 'required|string|min:2|max:255',
'birth_date' => ['required', new IsAdult(trans('Vous devez être majeur pour vous inscrire.'))],
'country' => [
'required',
new Enum(Country::class),
Rule::in($settings->getAuthorizedCountries()->pluck('value')->toArray()),
],
'email' => [
'required',
'email:rfc,dns,spoof',
'max:255',
// * Does email already exists in CRM ?
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundBeforeLastUsed
function (string $attribute, $value, Closure $fail): void {
if (CrmCustomer::findWithEmail(\strval($value)) instanceof CrmCustomer) {
$fail('Un compte existe déjà avec cette adresse.');
} else {
// ! Si le compte n'existe pas en CRM mais existe en local.
// ! On le soft-delete pour permettre l'inscription et la restauration propre.
$localCustomer = Customer::where('email', \strval($value))->first();
if ($localCustomer) {
$localCustomer->delete();
}
}
},
Rule::unique('customers', 'email')->withoutTrashed(),
],
'password' => [
'required',
'confirmed',
Password::min(8)->mixedCase()
->numbers()->symbols()->uncompromised(),
],
'ok_cgv' => 'accepted',
'opt_in' => 'required|boolean',
];
}
/**
* Get custom attributes for validator errors.
*/
public function attributes(): array
{
return [
'lastname' => trans('validation.attributes.first_name'),
'firstname' => trans('validation.attributes.first_name'),
'phone' => trans('validation.attributes.phone'),
'address' => trans('validation.attributes.line_address_1'),
'address2' => trans('validation.attributes.line_address_2'),
'country' => trans('validation.attributes.country'),
'postal_code' => trans('validation.attributes.postal_code'),
'city' => trans('validation.attributes.city'),
'birth_date' => trans('validation.attributes.date_of_birth'),
'email' => trans('validation.attributes.email'),
'password' => trans('validation.attributes.password'),
'password_confirmation' => trans('validation.attributes.password_confirmation'),
'ok_cgv' => trans('conditions d\'utilisation'),
'opt_in' => trans('newsletter'),
];
}
/**
* Get custom messages for validator errors.
*/
public function messages(): array
{
$messages = [
'postal_code' => trans('validation.postal_code', [
'countries' => \implode(', ', Country::getValidCountriesLabel()),
'examples' => 72000,
]),
'phone.phone' => trans('validation.phone', [
'countries' => \implode(', ', Country::getValidCountriesLabel()),
]),
'email.unique' => trans('Un compte existe déjà avec cette adresse.'),
'ok_cgv.accepted' => trans('Vous devez accepter les conditions d\'utilisation du site pour continuer.'),
]; // end match
return match ($this->integer('country')) {
Country::belgium->value => array_merge(
$messages,
[
'postal_code' => trans('validation.postal_code', [
'countries' => Country::belgium->label(),
'examples' => 1000,
]),
]
),
Country::luxembourg->value => array_merge(
$messages,
[
'postal_code' => trans('validation.postal_code', [
'countries' => Country::luxembourg->label(),
'examples' => 1111,
]),
]
),
Country::germany->value => array_merge(
$messages,
[
'postal_code' => trans('validation.postal_code', [
'countries' => Country::germany->label(),
'examples' => 10115,
]),
]
),
default => array_merge(
$messages,
[
'postal_code' => trans('validation.postal_code', [
'countries' => \implode(', ', Arr::except(
Country::getValidCountriesLabel(),
[
array_search(Country::belgium->label(), Country::getValidCountriesLabel()),
array_search(Country::luxembourg->label(), Country::getValidCountriesLabel()),
array_search(Country::germany->label(), Country::getValidCountriesLabel()),
]
)),
'examples' => 72000,
]),
]
),
};
}
}