ldc-legaulois/app/Http/Controllers/Fo/HomeController.php
2024-04-10 16:47:04 +02:00

57 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers\Fo;
use App\Http\Controllers\Controller;
use App\Models\ApproachSlide;
use App\Models\MainSlide;
use App\Http\Requests\Fo\SingleOptinRequest;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Models\SingleOptin;
class HomeController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Contracts\View\View
*/
public function index(): \Illuminate\Contracts\View\View
{
$mainSlideModels = MainSlide::query()->where('published', true)->orderBy('order', 'ASC')->get();
$approachSlideModels = ApproachSlide::query()->where('published', true)->orderBy('order', 'ASC')->get();
return view('fo.pages.home', compact('mainSlideModels', 'approachSlideModels'));
}
/**
* Call the CRM to add a single optin
*
* @param \App\Http\Requests\Fo\SingleOptinRequest $request
* @return string|array|null|\Illuminate\Contracts\Translation\Translator
* @throws \LDC\LDCCRM\Exceptions\ValidationException ValidationException.
* @throws \Illuminate\Contracts\Container\BindingResolutionException BindingResolutionException.
*/
public function addOptIn(SingleOptinRequest $request)
{
try {
SingleOptin::setStatus($request->email, true);
return \response(trans("Votre inscription à la newsletter a été prise en compte"), 200);
} catch (ApiErrorException $e) {
// Il y a deja un customerInfo avec optin a true pour ce customer.
if ($e->getApiError()->equals(ApiError::singleOptinCustomerExists())) {
return trans('Vous êtes déjà inscrit avec cette adresse email');
}
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
} catch (CrmException $e) {
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
return \response(trans('Une erreur est survenue, merci de réessayer ultérieurement'), 404);
}
}
}