ldc-cocotte/app/Http/Controllers/Fo/Customers/PasswordController.php
2026-10-27 09:58:12 +01:00

60 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Fo\Customers;
use App\Http\Controllers\Controller;
use App\Http\Requests\Fo\Customer\UpdatePasswordRequest;
use App\Models\Customer;
use App\Services\CrmService;
use Domain\Models\ValueObjects\CrmCustomer;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
class PasswordController extends Controller
{
public function __construct(
private readonly AppCustomerAuthService $authService,
private readonly CrmService $crmService,
) {
}
public function editPassword(): View
{
$this->authorize('manageOwnProfile', Customer::class);
return view('fo.customer.edit.password');
}
public function editPasswordSubmit(UpdatePasswordRequest $request): RedirectResponse
{
$this->authorize('manageOwnProfile', Customer::class);
$customer = $this->authService->getCustomerAsMandatory();
$crmCustomer = $this->crmService->getCustomerFromHashCode($customer->hashcode);
if (! $crmCustomer instanceof CrmCustomer) {
return back()->withErrors(trans('An error occurred, please try again later.'));
}
try {
$result = $this->crmService->changePassword(
$crmCustomer,
$request->string('password_old')->value(),
$request->string('password')->value()
);
if ($result instanceof CrmCustomer) {
return back()->with('success', trans('Password updated'));
}
return back()->withErrors($result->errors());
} catch (\Throwable $e) {
report($e);
return back()->withErrors(trans('An error occurred, please try again later.'));
}
}
}