Server : Apache System : Linux 145.162.205.92.host.secureserver.net 5.14.0-611.45.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 1 05:56:53 EDT 2026 x86_64 User : tradze ( 1001) PHP Version : 8.1.34 Disable Function : NONE Directory : /home/tradze/.trash/app_old/ |
<?php
namespace App;
use App\Modules\Users\Models\UserProfile;
use Bican\Roles\Models\Role;
use Carbon\Carbon;
use Intervention\Image\Facades\Image;
use Laravel\Socialite\Contracts\Provider;
use phpDocumentor\Reflection\File;
class SocialAccountService
{
/**
* Create new customer user or get one using socialite
* @param Provider $provider
* @return static
*/
public function createOrGetUser(Provider $provider)
{
$providerUser = $provider->user();
$providerName = class_basename($provider);
$contents = file_get_contents($providerUser->getAvatar());
//search account
$account = SocialAccount::whereProvider($providerName)
->whereProviderUserId($providerUser->getId())
->first();
//if account exists, return it
if ($account)
{
//get existing user
$user = $account->user;
//extension
$ext = pathinfo($providerUser->getAvatar(), PATHINFO_EXTENSION)?:'jpg';
//filename
$filename = strtolower(str_slug($user->name)).'.'.$ext;
$file = file_get_contents($providerUser->getAvatar());
$save = file_put_contents(public_path('images/avatar/').$filename,$file);
//update image profile
$profile = $user->profile;
if (!$profile){
$username = explode(' ',$user->name);
//create new profile
$profile = UserProfile::create([
'user_id' => $user->id,
'first_name' => $username[0],
'last_name' => $username[1],
]);
} //endif create new user profile
//save avatar to profile
$profile->avatar = $save?'avatar/'.$filename:'';
$profile->save();
$img = Image::make(public_path('images/'.$profile->avatar))->fit(400,400,null,'top')->save(public_path('images/'.$profile->avatar),100);
//return user
return $user;
}
else
{
//social account does not exist
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => $providerName
]);
//search user by email
$user = User::whereEmail($providerUser->getEmail())->first();
//if user does not exists, create new one and attach customer role
if (!$user) {
//create new user and set trial end date
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
'trial_ends_at' => Carbon::now()->addYears(10),
]);
$name = explode(' ',$user->name);
//extension
$ext = pathinfo($providerUser->getAvatar(), PATHINFO_EXTENSION)?:'jpg';
//filename
$filename = strtolower(str_slug($user->name)).'.'.$ext;
$file = file_get_contents($providerUser->getAvatar());
$save = file_put_contents(public_path('images/avatar/').$filename,$file);
$profile = UserProfile::create([
'user_id' => $user->id,
'first_name' => $name[0],
'last_name' => $name[1],
'avatar' => $save?('avatar/'.$filename):'',
]);
$img = Image::make(public_path('images/'.$profile->avatar))->fit(400,400,null,'top')->save(public_path('images/'.$profile->avatar),100);
//get customer role
$role = Role::where('slug','customer')->first();
//attach role
$user->attachRole($role);
} //endif
else{
//extension
$ext = pathinfo($providerUser->getAvatar(), PATHINFO_EXTENSION)?:'jpg';
//filename
$filename = strtolower(str_slug($user->name)).'.'.$ext;
$file = file_get_contents($providerUser->getAvatar());
$save = file_put_contents(public_path('images/avatar/').$filename,$file);
//update image profile
$profile = $user->profile;
if (!$profile){
$username = explode(' ',$user->name);
//create new profile
$profile = UserProfile::create([
'user_id' => $user->id,
'first_name' => $username[0],
'last_name' => $username[1],
]);
} //endif create new user profile
//save avatar to profile
$profile->avatar = $save?'avatar/'.$filename:'';
$profile->save();
$img = Image::make(public_path('images/'.$profile->avatar))->fit(400,400,null,'top')->save(public_path('images/'.$profile->avatar),100);
}
//associate social account to user
$account->user()->associate($user);
$account->save();
//return user
return $user;
} //end elseif
} //end method
} //end class