ldc-legaulois/app/Http/Controllers/Fo/ConsentController.php
2026-03-03 17:22:33 +01:00

41 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers\Fo;
use App\Models\OptinConsent;
use LDC\LDCCRM\Models\SingleOptin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\ConsentRequest;
use LDC\LDCCRM\Exceptions\CrmException;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Exceptions\ValidationException;
class ConsentController extends Controller
{
/**
* Check if a customer consent to receive newsletters.
*
* @param \App\Http\Requests\Fo\ConsentRequest $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
* @throws \LDC\LDCCRM\Exceptions\ValidationException ValidationException.
* @throws \LDC\LDCCRM\Exceptions\CrmException CrmException
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException ApiErrorException
*/
public function consent(ConsentRequest $request): \Illuminate\Contracts\View\View
{
$isSubscribed = false;
if ($request->action === 'optin') {
try {
if ($optinToAdd = OptinConsent::query()->where('hashcode', $request->hashcode)->first()) {
SingleOptin::setStatus($optinToAdd->email, true);
$isSubscribed = true;
}
} catch (ValidationException | CrmException | ApiErrorException $e) {
report($e);
}
}
return view('fo.pages.newsletter-consent', compact('isSubscribed'));
}
}