157 lines
5.3 KiB
PHP
157 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Fo\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Fo\Customers\CreateAccountRequest;
|
|
use App\Models\Customer;
|
|
use Illuminate\Contracts\Validation\Validator as ValidationValidator;
|
|
use Illuminate\Foundation\Auth\RegistersUsers;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException as IlluminateValidationException;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Exceptions\ValidationException as CrmValidationException;
|
|
use LDC\LDCCRM\Models\Customer as CrmCustomer;
|
|
|
|
class RegisterController extends Controller
|
|
{
|
|
use RegistersUsers;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('guest:frontend');
|
|
}
|
|
|
|
/**
|
|
* Get the guard to be used during authentication.
|
|
*
|
|
* @return \Illuminate\Contracts\Auth\StatefulGuard
|
|
*/
|
|
protected function guard()
|
|
{
|
|
return Auth::guard('frontend');
|
|
}
|
|
|
|
/**
|
|
* Show the application registration form.
|
|
*
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function showRegistrationForm()
|
|
{
|
|
return view('theme::auth.register');
|
|
}
|
|
|
|
/**
|
|
* Get a validator for an incoming registration request.
|
|
*
|
|
* @param array $data
|
|
* @return \Illuminate\Contracts\Validation\Validator
|
|
* @throws IlluminateValidationException If data is incorrect.
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
*/
|
|
protected function validator(array $data): ValidationValidator
|
|
{
|
|
// phpcs:enable
|
|
$request = CreateAccountRequest::capture();
|
|
$request->prepareForValidation();
|
|
return Validator::make(
|
|
$request->validationData(),
|
|
$request->rules(),
|
|
$request->messages(),
|
|
$request->attributes()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Create a new user instance after a valid registration.
|
|
*
|
|
* @param array $data
|
|
* @return \App\Models\Customer
|
|
* @throws IlluminateValidationException If there are validation errors.
|
|
* @throws \RuntimeException On unhandled api Error.
|
|
*/
|
|
protected function create(array $data): Customer
|
|
{
|
|
$data['ok_cgv'] = \request()->boolean('ok_cgv');
|
|
$data['opt_in'] = \request()->boolean('opt_in');
|
|
$data['phone'] = Arr::get($data, 'phone');
|
|
// * This field is not standard in crm
|
|
$data['postcode'] = Arr::get($data, 'postal_code');
|
|
Arr::forget($data, 'postal_code');
|
|
|
|
try {
|
|
return DB::transaction(function () use ($data) {
|
|
$crmCustomer = new CrmCustomer(Arr::only(
|
|
$data,
|
|
[
|
|
'firstname', 'lastname', 'birth_date', 'address',
|
|
'address2', 'postcode', 'country', 'city', 'phone',
|
|
'email', 'password', 'opt_in',
|
|
]
|
|
));
|
|
|
|
$crmCustomer->save();
|
|
|
|
// * Enregistrement du Hashcode
|
|
/** @var \App\Models\Customer $customer */
|
|
$customer = Customer::withTrashed()->firstOrNew([
|
|
'email' => $data['email']
|
|
]);
|
|
$customer->fill(Arr::only($data, $customer->getFillable()));
|
|
$customer->hashcode = $crmCustomer->hashcode;
|
|
if ($customer->trashed()) {
|
|
$customer->restore();
|
|
}
|
|
$customer->email_verified_at = $crmCustomer->status ? now() : null;
|
|
// ! Last connection sera null, pour identifier la deuxième connexion,
|
|
// ! Pour accéder au partie du Klub.
|
|
if ($customer->saveOrFail()) {
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
Session::flash('success', trans('front.messages.account_created_email_confirmation_sent'));
|
|
}
|
|
|
|
return $customer;
|
|
});
|
|
} catch (ApiErrorException $e) {
|
|
if ($e->getApiError()->equals(ApiError::customerExists)) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => trans('front.messages.email_already_in_use')
|
|
]);
|
|
}
|
|
if ($e->getApiError()->equals(ApiError::validationFailed)) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
report($e);
|
|
Log::error(__CLASS__ . '::create reached end of possibilities.');
|
|
throw new \RuntimeException(__CLASS__ . '::create reached end of possibilities.');
|
|
} catch (CrmValidationException $e) {
|
|
throw IlluminateValidationException::withMessages([
|
|
'error' => $e->getMessage()
|
|
]);
|
|
} //end try
|
|
}
|
|
|
|
/**
|
|
* Get the post register / login redirect path.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function redirectPath()
|
|
{
|
|
return $this->authLoggedRedirectPath();
|
|
}
|
|
}
|