56 lines
1.7 KiB
PHP
56 lines
1.7 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.
|
|
*
|
|
* @param \LDC\LDCCRM\Models\Customer $crmCustomer
|
|
* @return void
|
|
*/
|
|
public function __construct(Customer $crmCustomer)
|
|
{
|
|
$this->crmCustomer = $crmCustomer;
|
|
}
|
|
|
|
/**
|
|
* Run the validation rule.
|
|
*
|
|
* @param string $attribute
|
|
* @param mixed $value
|
|
* @param \Closure(string):\Illuminate\Translation\PotentiallyTranslatedString $fail
|
|
* @return void
|
|
* @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;
|
|
}
|
|
}
|
|
}
|
|
}
|