64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Illuminate\Contracts\Validation\Rule;
|
|
use LDC\LDCCRM\Enums\ApiError;
|
|
use LDC\LDCCRM\Exceptions\ApiErrorException;
|
|
use LDC\LDCCRM\Models\Customer;
|
|
|
|
class CrmCustomerPassword implements Rule
|
|
{
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* Determine if the validation rule passes.
|
|
*
|
|
* @param mixed $attribute
|
|
* @param mixed $value
|
|
* @return boolean
|
|
* @throws \LDC\LDCCRM\Exceptions\ApiErrorException If any unhandled error occurs.
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed
|
|
*/
|
|
public function passes($attribute, $value)
|
|
{
|
|
// phpcs:enable
|
|
try {
|
|
return Customer::login($this->crmCustomer->email, strval($value));
|
|
} catch (ApiErrorException $e) {
|
|
if ($e->getApiError()->equals(ApiError::authFailed)) {
|
|
return false;
|
|
}
|
|
throw $e;
|
|
}
|
|
|
|
// Pass.
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation error message.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function message()
|
|
{
|
|
return trans('Votre ancien mot de passe n\'as pu être vérifié');
|
|
}
|
|
}
|