ldc-legaulois/app/Rules/CrmCustomerPassword.php
2026-09-25 16:25:31 +02:00

55 lines
1.5 KiB
PHP

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\ValidationRule;
use LDC\LDCCRM\Enums\ApiError;
use LDC\LDCCRM\Exceptions\ApiErrorException;
use LDC\LDCCRM\Models\Customer;
class CrmCustomerPassword implements ValidationRule
{
/**
* Crm Customer
*
* @var \LDC\LDCCRM\Models\Customer
*/
private $crmCustomer;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(Customer $crmCustomer)
{
$this->crmCustomer = $crmCustomer;
}
/**
* Run the validation rule.
*
* @param \Closure(string):\Illuminate\Translation\PotentiallyTranslatedString $fail
*
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing
*
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
*/
public function validate(string $attribute, mixed $value, \Closure $fail): void
{
// phpcs:enable
try {
if (! Customer::login($this->crmCustomer->email, strval($value))) {
$fail(trans('Votre ancien mot de passe n\'as pu être vérifié'));
}
} catch (ApiErrorException $e) {
if ($e->getApiError()->equals(ApiError::authFailed)) {
$fail($e->getMessage());
} else {
throw $e;
}
}
}
}