getCustomer($credentials['email']); if (!$crmCustomer) { // * Customer is not found on CRM. return null; } $model = $this->createLocalCustomer($crmCustomer); } return $model; } catch (ApiErrorException $e) { // * Customer is not found. if ($e->getApiError()->equals(ApiError::modelNotFound())) { return null; } report($e); Log::error('Unhandled response ', $e->getApiError()->label); } catch (CrmException | \Exception $e) { report($e); // Une erreur critique est survenue avec la CRM. return null; } //end try Log::error(__CLASS__ . '::retrieveByCredentials reached end of possibilities.'); return null; } /** * Validate a user against the given credentials. * * @param UserContract $model * @param array $credentials * @return boolean * @throws \RuntimeException If model provided is not a Customer or connection to server fails. * @throws \LDC\LDCCRM\Exceptions\CrmException On CRM call error. * @throws \Exception On CRM call error. */ public function validateCredentials(UserContract $model, array $credentials) { // FIXME: handle customerNotActivated /** @var \Illuminate\Database\Eloquent\Model $model */ if (\get_class($model) !== $this->usedModel) { throw new \RuntimeException('Cannot authenticate any thing else than ' . $this->usedModel); } try { /** @var \LDC\LDCCRM\CrmApiV2 */ $crm = app('crmAPI'); // * Ne devrait pas survenir, $crm->login throws ApiError::authFailed() return $crm->login($credentials['email'], $credentials['password']); } catch (ApiErrorException $e) { if ($e->getApiError()->equals(ApiError::authFailed())) { return false; } } catch (CrmException | \Exception $e) { report($e); if (config('app.debug')) { throw $e; } return false; } //end try Log::error(__CLASS__ . '::validateCredentials reached end of possibilities.'); return null; } /** * Create a local customer * * @param \LDC\LDCCRM\Models\Customer $crmCustomer * @return \Illuminate\Database\Eloquent\Model * @throws \Throwable If saving to DB failed. */ private function createLocalCustomer(\LDC\LDCCRM\Models\Customer $crmCustomer): Model { /** @var \Illuminate\Database\Eloquent\Model $model */ $model = $this->usedModel::firstOrNew([ 'email' => $crmCustomer->email ]); $model->hashcode = $crmCustomer->hashcode; $model->saveOrFail(); return $model; } }