/** * Static Functions */ public static function processExternal($provider, $user_profile, User $user = null) { $external = self::getRepository()->findOneBy(array('provider' => $provider, 'external_id' => $user_profile->identifier)); // Locate a user account to associate. if ($user instanceof User) { // No additional processing. } elseif ($external instanceof self && $external->user instanceof User) { $user = $external->user; } elseif (!empty($user_profile->email)) { $user = User::getRepository()->findOneBy(array('email' => $user_profile->email)); if (!$user instanceof User) { $user = new User(); $user->email = $user_profile->email; $user->name = $user_profile->displayName; $user->avatar_url = $user_profile->photoURL; $user->generateRandomPassword(); $user->save(); } } else { // Not enough information to auto-create account; throw exception. throw new \PVL\Exception\AccountNotLinked(); } // Create new external record (if none exists) if (!$external instanceof self) { // Create new external account and associate with the specified user. $external = new self(); $external->provider = $provider; $external->external_id = $user_profile->identifier; } $external->user = $user; $external->name = $user_profile->displayName; $external->avatar_url = $user_profile->photoURL; $external->save(); return $user; }