protected function _getUser($adapter)
{
try {
$providerProfile = $adapter->getUserProfile();
$this->adapter($adapter);
$this->profile($providerProfile);
} catch (\Exception $e) {
$adapter->logout();
throw $e;
}
$config = $this->_config;
$userModel = $this->_userModel;
$user = null;
$profile = $this->_query($providerProfile->identifier)->first();
if ($profile) {
$userId = $profile->get($config['profileModelFkField']);
$user = $this->_userModel->find($config['finder'])->where([$userModel->aliasField($userModel->primaryKey()) => $userId])->first();
// User record exists but finder conditions did not match,
// so just update social profile record and return false.
if (!$user) {
$profile = $this->_profileEntity($profile);
if (!$this->_profileModel->save($profile)) {
throw new \RuntimeException('Unable to save social profile.');
}
return false;
}
} elseif ($providerProfile->email) {
$user = $this->_userModel->find($config['finder'])->where([$this->_userModel->aliasField($config['fields']['email']) => $providerProfile->email])->first();
}
$profile = $this->_profileEntity($profile);
if (!$user) {
$user = $this->_newUser($profile);
}
$profile->{$config['profileModelFkField']} = $user->{$userModel->primaryKey()};
$profile = $this->_profileModel->save($profile);
if (!$profile) {
throw new \RuntimeException('Unable to save social profile.');
}
$user->set('social_profile', $profile);
$user->unsetProperty($config['fields']['password']);
return $user->toArray();
}